kundera icon indicating copy to clipboard operation
kundera copied to clipboard

Kundera will not scan entities from a JAR file

Open dstrawson opened this issue 14 years ago • 28 comments

Hi -

Not sure if this is a bug or a feature request. If you take the view that this is a feature then I understand!

My entities are in a separate file (core.jar) which is included in my application as a dependency. This is because it is shared between modules. Kundera doesn't find them - neither when I use in the persistence.xml file, nor when I use kundera.annotations.scan.package. As soon as I un-jar the core.jar file into my classes/ folder, everything starts working as expected.

Setup:

Kundera-2.0.7 Cassandra-1.1.2 Maven Sping / CXF

dstrawson avatar Jul 27 '12 17:07 dstrawson

Did you try adding jar file undera "jar-file" tag in your persistence.xml?

-Vivek

mevivs avatar Jul 27 '12 18:07 mevivs

Hi Vivek -

Thanks - I admit I wasn't aware of that option - however its not ideal as the name and location of the class file in production will change, so its not the best solution I feel. My jar file will move around between development, staging and live servers, and change name between versions, although I do realise of course that I could alleviate the version issue by filtering the files at build time in maven.

Also, according to the jpa spec, my reading is that should be enough to find classes, and jar-file is used to search specific jar-files rather in the same way as kundera.annotations.scan.package, these jar files should also be specified relative to the directory or jar that contains the root of the persistence unit, so for example lib/core-1.2.3-SNAPSHOT.jar.

Daniel

dstrawson avatar Jul 28 '12 08:07 dstrawson

Hi, As per JPA: Any entity class present within root folder of( where your persistence.xml resides) will be automatically scanned and loaded, else you need to define those as part of "mapping-class" tag OR "jar-file" tag.

Here are some excerpt about loading classes from JPA specification:


/**

  • Returns the list of the names of the mapping files that the
  • persistence provider must load to determine the mappings for
  • the entity classes. The mapping files must be in the standard
  • XML mapping format, be uniquely named and be resource-loadable
  • from the application classpath. Each mapping file name
  • corresponds to a mapping-file element in the
  • persistence.xml file.
  • @return the list of mapping file names that the persistence
  • provider must load to determine the mappings for the entity
  • classes */ public List getMappingFileNames();

/**

  • Returns a list of URLs for the jar files or exploded jar
  • file directories that the persistence provider must examine
  • for managed classes of the persistence unit. Each URL
  • corresponds to a jar-file element in the
  • persistence.xml file. A URL will either be a file: URL
  • referring to a jar file or referring to a directory
  • that contains an exploded jar file, or some other URL from
  • which an InputStream in jar format can be obtained.
  • @return a list of URL objects referring to jar files or
  • directories */ public List getJarFileUrls();

/**

  • Returns the URL for the jar file or directory that is the
  • root of the persistence unit. (If the persistence unit is
  • rooted in the WEB-INF/classes directory, this will be the
  • URL of that directory.)
  • The URL will either be a file: URL referring to a jar file
  • or referring to a directory that contains an exploded jar
  • file, or some other URL from which an InputStream in jar
  • format can be obtained.
  • @return a URL referring to a jar file or directory */ public URL getPersistenceUnitRootUrl();

Scanning complete classpath for entities is somewhat not desirable.

I hope am not missing anything on this front. Feel free to correct, if i missed something.

-Vivek

mevivs avatar Jul 28 '12 09:07 mevivs

I'm reading persistence-2_0-final-spec.pdf - specifically 8.2.1.6.3 and 8.2.1.6.4.

"A list of named managed persistence classes may be specified instead of, or in addition to, the JAR files and mapping files. Any mapping metadata annotations found on these classes will be processed ... The class element is used to list a managed persistence class."

Where was the above from?

I agree that scanning complete classpaths isn't desirable (although I've never had this problem before with say hibernate) - however I'd hope not to have to name the jar files either. Ideally I should just list the class files that contain my entities, which would be loaded as normal. My reading of the spec is that this is allowed.

dstrawson avatar Jul 28 '12 09:07 dstrawson

It was from 9.5 section. i am on same spec :)

As you mentioned for 8.2.1.x section, here it is:

"The managed persistence classes may either be contained within the root of the persistence unit; or they may be specified by reference—i.e., by naming the classes, class archives, or XML mapping files (which in turn reference classes) that are accessible on the application classpath; or they may be speci- fied by some combination of these means. See Section 8.2.1.6. " (this is from "8.2.1 persistence.xml file" section). "The managed persistence classes may either be contained within the root of the persistence unit" , i state it as for example your persistence.xml is at "classes/META-INF" folder, so root would become to " classes" folder to automatically scan for "managed persistence classes" else you need to explicitly configure them. That is what my understanding is from this point.

With Hibernate, did you work in annotation way or defined any orm.xml or hbm.xml ? As my understanding is without them or not defining them within "mapping-class" or "mapping-file" tag, it should complain about it, unless "managed instances" are not in the root folder of persistence.xml

Would love to hear your point on this.

-Vivek

mevivs avatar Jul 28 '12 09:07 mevivs

Just noticed that you mentioned: "Ideally I should just list the class files that contain my entities, which would be loaded as normal. My reading of the spec is that this is allowed"

That is what i am saying, should be the way. So if you have defined "mapping-class" and mapped your managed instances with them, they should automatically get loaded, provided your "jar" containing them should be in classpath!

-Vivek

mevivs avatar Jul 28 '12 10:07 mevivs

Please share your persistence.xml. Does it contain "class" tag mapping for managed instances (available in core.jar). Then Kundera should be able to load them provided that those jars are available with "Thread.currentThread().getContextClassLoader()".

Or Your project is based on OSGI module based class loading?

mevivs avatar Jul 28 '12 10:07 mevivs

For a moment there I thought I'd done something dumb like not included the jar in the classpath, however it would never reach persist() if I had. Not knowingly using OSGI, no - I'm using spring, but spring-modules isn't involved.

Given what you are saying I'm worried that I've done something wrong, so I'll build a much smaller simpler example to replicate if that's ok, as I think I might be wasting your time otherwise,

My persistence.xml file:

<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
    http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
    version="2.0">
    <persistence-unit name="cassandra">
        <provider>com.impetus.kundera.KunderaPersistence</provider>
        <class>co.twickets.core.Application</class>
        <properties>            
            <property name="kundera.nodes" value="localhost"/>
            <property name="kundera.port" value="9160"/>
            <property name="kundera.keyspace" value="Twickets"/>
            <property name="kundera.dialect" value="cassandra"/>
            <property name="kundera.client.lookup.class" value="com.impetus.client.cassandra.pelops.PelopsClientFactory" />
            <property name="kundera.annotations.scan.package" value="co.twickets.core" />
            <property name="kundera.ddl.auto.prepare" value="update"/>
        </properties>       
    </persistence-unit>
</persistence>

dstrawson avatar Jul 28 '12 11:07 dstrawson

Hi Daniel, This configuration looks perfectly FINE to me.

Can you share your source code to have a look and test it at my end please?

-Vivek

mevivs avatar Jul 28 '12 11:07 mevivs

Hi Daniel, I think i have found out the issue. Looks like there is bug within Kundera. I will update you soon on this.

-Vivek

mevivs avatar Jul 28 '12 11:07 mevivs

Could you please try with latest "trunk". I have added a fix for this.

Please verify.

-Vivek

mevivs avatar Jul 28 '12 11:07 mevivs

Great - I've actually just built a smaller example to show the issue.

I'll try it with the trunk as you suggest.

Thanks for your help by the way :)

dstrawson avatar Jul 28 '12 15:07 dstrawson

Thanks to you for pointing towards the issue :) Process was as per jpa spec, but Kundera missed out those scanned classes. It should work now.

-Vivek

mevivs avatar Jul 28 '12 15:07 mevivs

OK, I've pushed an example to https://github.com/dstrawson/kunderatest. It also demonstrates issue 91, although I've not gone back to that yet, which I will do when I have this working.

I've pulled trunk / built / installed your new version and updated my dependencies / rebuilt but not managed to get it working with this example, still the same issue - when I built it for the first time it failed a UT however, so will need to go back and see why it failed the test.

dstrawson avatar Jul 28 '12 16:07 dstrawson

Thanks. Will have a look and get back to you.

-Vivek

mevivs avatar Jul 28 '12 16:07 mevivs

Hi Vivek -

When I build kundera-cassandra 2.0.8-SNAPSHOT I get the following :

<<< FAILURE! Running com.impetus.client.twitter.TwissandraTest com.impetus.kundera.loader.MetamodelLoaderException: Name conflict between classes com.impetus.client.crud.countercolumns.Counters and com.impetus.client.crud.countercolumns.Counters. Make sure no two entity classes with the same name are specified for persistence unit twissandraTest at com.impetus.kundera.configure.MetamodelConfiguration.scanClassAndPutMetadata(MetamodelConfiguration.java:303) at com.impetus.kundera.configure.MetamodelConfiguration.loadEntityMetadata(MetamodelConfiguration.java:212) at com.impetus.kundera.configure.MetamodelConfiguration.configure(MetamodelConfiguration.java:97)

dstrawson avatar Jul 28 '12 16:07 dstrawson

Try with mvn clean install -Dmaven.test.skip=true

mevivs avatar Jul 28 '12 16:07 mevivs

Sure, that works, however I cannot get that fix to show itself I'm afraid so was assuming that the broken test might have something to do with it.

dstrawson avatar Jul 28 '12 16:07 dstrawson

There is where I am with 2.0.8-SNAPSHOT:

  • If I only include elements in persistence.xml Kundera fails to pick up the entity classes. I think If I understood you correctly you were saying that this should work ok.?
  • If I additionally add jar-file it works ok eg ../core-package/target/core.jar - as I say above not ideal for production as will reference the version, but we could do something about that with a maven resource filter, so workable.
  • I can also unpack the jar file, where again it works ok, so there is another option there.

dstrawson avatar Jul 29 '12 09:07 dstrawson

If I only include elements in persistence.xml Kundera fails to pick up the entity classes. I think If I understood you correctly you were saying that this should work ok.?

Yes. This is what i believe should work. i will have a look with your sample example.

Till then, as a workaround other 2 options should be workable for you.

-Vivek

mevivs avatar Jul 29 '12 11:07 mevivs