Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hibernate jpa unit tests autodection does not work

Tags:

jpa

I have the following in persistence.xml

<persistence-unit name="test" transaction-type="RESOURCE_LOCAL">
    <class>com.merc.model.log.EventLogging</class>
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
    <properties>
        <!-- Scan for annotated classes and Hibernate mapping XML files -->
        <property name="hibernate.archive.autodetection" value="class"/>
    </properties>
</persistence-unit>

If I comment out com.merc.model.log.EventLogging, I get Unknown entity exception.

Any ideas as to why autodetection would not work

like image 319
user373201 Avatar asked Jan 18 '11 16:01

user373201


1 Answers

This can be caused by the fact that by default autodetection works for classes inside the same classpath item where persistence.xml is located.

So, you have separate target folders for the code itself and for the tests (for example, if you use Maven with default configuration), and if this persistence.xml ends up in tests' target folder after compilation, classes from the main target folder wouldn't be detected.

You can use <jar-file> elements to point to other classpath items that should be searched for entities.

If you use Maven, you can do it in elegant way using resource filtering:

persistence.xml:

<jar-file>${project.build.outputDirectory}</jar-file>

pom.xml:

<build>
    <testResources>
        <testResource>
            <directory>src/test/resources</directory>
            <filtering>true</filtering>
        </testResource>
    </testResources>
</build>
like image 184
axtavt Avatar answered Oct 20 '22 00:10

axtavt