Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to auto-register entities with JPA/Hibernate: Unknown entity

I'm stumped with a Hibernate/JPA configuration issue that's preventing my JPA-annotated entities from being automatically registered:

java.lang.IllegalArgumentException: Unknown entity: com.example.crm.server.model.Language
    at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:671)
    at com.example.crm.server.model.Language.persist(Language.java:64)
    at com.example.crm.server.LanguageTest.testPersistAndRemove(LanguageTest.java:32)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

In my entity class I have:

@Entity
@Table(name="Languages")
public class Language implements Serializable
{
    @Id
    private Long id;
    private String name;
    // etc...
}

And in MySQL, the Languages table looks like:

+-------------+----------+------+-----+---------+-------+
| Field       | Type     | Null | Key | Default | Extra |
+-------------+----------+------+-----+---------+-------+
| Language_ID | int(11)  | NO   | PRI | NULL    |       | 
| Name        | char(18) | YES  |     | NULL    |       | 
+-------------+----------+------+-----+---------+-------+
2 rows in set (0.00 sec)

And my persistence.xml looks like:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
             version="1.0">

    <persistence-unit name="crm">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>

        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
            <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
            <property name="hibernate.connection.url" value="jdbc:mysql://localhost/crm"/>
            <property name="hibernate.connection.username" value="crmuser"/>
            <property name="hibernate.connection.password" value="mypass"/>
            <property name="hibernate.c3p0.min_size" value="5"/>
            <property name="hibernate.c3p0.max_size" value="20"/>
            <property name="hibernate.c3p0.idleTestPeriod" value="30"/>
            <property name="hibernate.c3p0.timeout" value="0"/>
            <property name="hibernate.c3p0.max_statements" value="0"/>
            <property name="hibernate.format_sql" value="true"/>
            <property name="hibernate.query.jpaql_strict_compliance" value="false"/>
            <property name="hibernate.validator.apply_to_ddl" value="false"/>
            <property name="hibernate.validator.autoregister_listeners" value="false"/>
            <property name="hibernate.archive.autodetection" value="class, hbm"/>
            <property name="hibernate.hbm2ddl.auto" value="create"/>
        </properties>
    </persistence-unit>

</persistence>

EDIT: here's how I'm getting my EntityManager and persisting:

public void persist()
{
    EntityManager em = entityManager();
    try
    {
        em.getTransaction().begin();
        em.persist(this);
        em.getTransaction().commit();
    }
    finally
    {
        em.close();
    }
}

public static EntityManager entityManager()
{
    return EMF.get().createEntityManager();
}
like image 586
George Armhold Avatar asked Feb 08 '11 23:02

George Armhold


People also ask

Can we have an entity without @ID JPA?

An entity must always have a primary key; you cannot create an entity without a primary key (id).

How do you indicate a JPA entity?

Each JPA entity must have a primary key that uniquely identifies it. The @Id annotation defines the primary key. We can generate the identifiers in different ways, which are specified by the @GeneratedValue annotation. If we specify GenerationType.

What does @entity mean in JPA?

Entities in JPA are nothing but POJOs representing data that can be persisted to the database. An entity represents a table stored in a database. Every instance of an entity represents a row in the table.

What is @ID annotation in hibernate?

The @Id annotation is inherited from javax.persistence.Id, indicating the member field below is the primary key of the current entity. Hence your Hibernate and spring framework as well as you can do some reflect works based on this annotation.


1 Answers

It turned out to be fairly simple: list the classes directly in the persistence.xml file. Both armandino and MikelRascher led me to this answer, though indirectly, so props to them.

Here is the persistence.xml I'm using now:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
             version="1.0">

    <persistence-unit name="crm">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>

        <class>com.example.Language</class>

        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
            <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
            <property name="hibernate.connection.url" value="jdbc:mysql://localhost/crm"/>
            <property name="hibernate.connection.username" value="myuser"/>
            <property name="hibernate.connection.password" value="mypass"/>
            <property name="hibernate.c3p0.min_size" value="5"/>
            <property name="hibernate.c3p0.max_size" value="20"/>
            <property name="hibernate.c3p0.idleTestPeriod" value="30"/>
            <property name="hibernate.c3p0.timeout" value="0"/>
            <property name="hibernate.c3p0.max_statements" value="0"/>
            <!--property name="hibernate.show_sql" value="true"/>-->
            <property name="hibernate.format_sql" value="true"/>
            <property name="hibernate.query.jpaql_strict_compliance" value="false"/>
            <property name="hibernate.validator.apply_to_ddl" value="false"/>
            <property name="hibernate.validator.autoregister_listeners" value="false"/>
            <property name="hibernate.archive.autodetection" value="class, hbm"/>
            <property name="hibernate.hbm2ddl.auto" value="create"/>
        </properties>
    </persistence-unit>

</persistence>
like image 149
George Armhold Avatar answered Sep 28 '22 18:09

George Armhold