I have module A and module B which both have JPA annotated classes. Module B has an unit test that pulls in a couple of those entities from A. Both modules compile fine, the runtime dependencies are set OK, but I get the following error when I try to run the unit test:
java.lang.IllegalArgumentException: Unknown entity: MyClassHere
Caused by: org.hibernate.MappingException: Unknown entity: MyClassHere
This occurs in the EntityManager.merge call.
Since module B has all the hibernate config files etc, I'm guessing it's simply not picking up that my class from A is an entity.
I tried adding the following to persistence.xml
<exclude-unlisted-classes>false</exclude-unlisted-classes>
In hibernate.cfg.xml I added:
<property name="packagesToScan">myNamespace.*</property>
Then:
<property name="packagesToScan">
<array>
<value>myNamespace.*</value>
</array>
</property>
That gave me an error that content of "property" must match null. Then I tried:
<mapping class="myNamespace.*" />
What am I missing?
Edit: One thing that I forgot to mention that might be of significance is that the two modules are set up as separate projects (I'm using eclipse) so the directory structure is different. The run time dependencies are all set up correctly but since the .class files end up in different directories, I think hibernate might not be scanning those.
hibernate mappings are one of the key features of hibernate . they establish the relationship between two database tables as attributes in your model. that allows you to easily navigate the associations in your model and criteria queries.
@Entity annotation marks this class as an entity. @Table annotation specifies the table name where data of this entity is to be persisted. If you don't use @Table annotation, hibernate will use the class name as the table name by default.
Entity object is an object of our plain old java class(POJO) Model/Business class, which needs to be persisted in a database using Hibernate, while Value Type object is an object of another class but it is stored as a part of Entity object within a database table.
If you configure your project to autodetect the entities, it will only scan the path where the META-INF/persistence.xml is (by default).
In addition to :
<exclude-unlisted-classes>false</exclude-unlisted-classes>
You set an extra hibernate option :
<property name="hibernate.archive.autodetection" value="class, hbm" />
It determines which element is auto discovered by Hibernate Entity Manager.
For extra entities (in others jars), you can set the jar-file section in your main persistence.xml file :
<persistence>
<persistence-unit name="myUnit">
...
<class>foo.bar.Entity1</class>
<class>foo.bar.Entity2</class>
<jar-file>moduleB.jar</jar-file>
...
</persistence-unit>
</persistence>
The jar-file element specifies JAR files that are visible to the packaged persistence unit that contain managed persistence classes, whereas the class element explicitly names managed persistence classes.
The JAR file or directory whose META-INF directory contains persistence.xml is called the root of the persistence unit. The scope of the persistence unit is determined by the persistence unit’s root. Each persistence unit must be identified with a name that is unique to the persistence unit’s scope.
Regards, André
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With