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.xmlis 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.xmlends 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:pom.xml: