0

I use JAVA SE and Hibernate. I have a package with annotated class name Message (with javax @Entity annotation). my persistence.xml is very simple:

<?xml version="1.0" encoding="UTF-8"?>

<persistence-unit
        name="myFirstJPA"
        transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    <properties>
        <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL10Dialect"/>
        <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver"/>
        <property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/testdb"/>
        <property name="javax.persistence.jdbc.user" value="postgres"/>
        <property name="javax.persistence.jdbc.password" value="mypassword"/>
    </properties>
</persistence-unit>

As far as I know Hibernate scans all the annotated class at startup in order to use them with entityManager. So when I want to persist simple entity via entitymanager.persist I get an error of type IllegalArgumentException: Unknown entity

only when I add the following to persistence.xml

        <class>com.example.entity.Message</class>

The exception goes away and everything works fine.

Since I have approximately 100 entities in my project I don't want to add each one of them to the persistence.xml file I want to Hibernate to detect them automatically. Any ideas?

user1409534
  • 2,140
  • 4
  • 27
  • 33
  • Could you, please, provide the whole persistence.xml. The one you have in the question now is not full, because `persistence-unit` is not an upper-level tag, it must be wrapped with the `persistence` tag. As an example, look here: https://vladmihalcea.com/jpa-persistence-xml/ – Dmitrii Bocharov Apr 26 '21 at 08:19
  • @user1409534 What hibernate version do you use? Do you define your entities in the same project (jar) that holds the `persistence.xml`? – SternK Apr 26 '21 at 09:46
  • I'm using the Hibernate version 5.4.30 yes my entity in in the same project. I only past of the xml. The full xml is wrapped with – user1409534 Apr 26 '21 at 11:39

0 Answers0