Why are my annotated JPA classes not detected implicitly?

My persistence.xmllooks like this:

<persistence>
  <persistence-unit name="test">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <class>com.XXX.Abc</class>
    <properties>
      <property name="hibernate.archive.autodetection" value="true" />
      ..
    </properties>
  </persistence-unit>
<persistence>

Everything works perfectly. When I remove the directive <class>, I get an exception from EntityManager.find(Abc.class, 1):

java.lang.IllegalArgumentException: Unknown entity: com.XXX.Abc

It seems that hibernate cannot detect my annotated classes, although I use @Entity.. Why?

+3
source share
3 answers

The value hibernate.archive.autodetectionis a list of csv elements that are automatically recognized by hibernation.

Try this instead:

<property name="hibernate.archive.autodetection" value="class, hbm"/>

additional literature

+8
source

..

<property name="hibernate.archive.autodetection" value="class" />   

+1

I think Hibernate is looking for classes in the same code as persistence.xml. So, for example, if you have persistence.xml in the folder and classes in a separate jar, Hibernate will not find them.

0
source

Source: https://habr.com/ru/post/1767121/


All Articles