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