Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure hibernate to scan for entities in a different module

Tags:

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.

like image 320
ventsyv Avatar asked Oct 23 '15 15:10

ventsyv


People also ask

What is entity Relationship mapping in hibernate?

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.

What is the use of @entity in hibernate?

@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.

What is entity object in hibernate?

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.


Video Answer


2 Answers

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é

like image 117
André Blaszczyk Avatar answered Sep 19 '22 08:09

André Blaszczyk


  • If you are using hibernate/spring we can extend the LocalSessionFactoryBean object and scan through the project to identify entity classes in the project.
  • Since you are saying two different projects, then try to write some build time utility to parse the two projects and created one entity xml file that solve your problem.
like image 23
BValluri Avatar answered Sep 22 '22 08:09

BValluri