Hibernate jpa unit tests autodection not working

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 on com.merc.model.log.EventLogging, I get an unknown entity exception.

Any ideas as to why auto-detection is not working

+5
source share
1 answer

This may be due to the fact that, by default, auto-detection works for classes inside the same pathpath element where it is located persistence.xml.

So, you have separate target folders for the code itself and for the tests (for example, if you use Maven with the default setting), and if this one persistence.xmlgets into the target folder of the tests after compilation, the classes from the main target folder will not be detected.

<jar-file>, pathpath, .

Maven, , :

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>
+13

All Articles