0

I am trying to get a javax.persistence running, but I get erros. I built up a little project for testing, creating an entity class, the persistence.xml, and the running process:

Entity class:

package glasses;

import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Hund implements Serializable {
    @Id
    private long id;
    private String name;
    private String typ;

    public long getId() {
        return this.id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getTyp() {
        return this.typ;
    }

    public void setTyp(String typ) {
        this.typ = typ;
    }
}

persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
  <persistence-unit name="GlassesPU" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <properties>
      <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/glasses?zeroDateTimeBehavior=convertToNull"/>
      <property name="javax.persistence.jdbc.password" value="mypwd"/>
      <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
      <property name="javax.persistence.jdbc.user" value="root"/>
      <property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
    </properties>
  </persistence-unit>
</persistence>

Error message:

Caused by: java.lang.IllegalArgumentException: Object: glasses.Hund@5d2e0422 is not a known entity type.
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.registerNewObjectForPersist(UnitOfWorkImpl.java:4228)
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.persist(EntityManagerImpl.java:496)
at glasses.FXMLDocumentController.handleButtonAction(FXMLDocumentController.java:48)
... 54 more

Does anyone know the problem? Is there anything wrong in the Hund-class? Or in the persistence.xml?

Socrates
  • 8,724
  • 25
  • 66
  • 113
  • can you show the complete Exception ? – Mehdi Feb 01 '14 at 10:59
  • 1
    Hey @Mehdi, I just tried to give you the error message into here, but stackoverflow won't accept 6000 characters. An answer below Sabuj Hassan gave a working answer to my problem. Even though now my project works, the outcome is not quite satisfactory. Do you have any idea how to solve it better than that? – Socrates Feb 01 '14 at 11:46

2 Answers2

1

Use your Hund class inside your persistence.xml like following example. Just place it between the properties and the persistence-unit tags.

</properties>
  <class>glasses.Hund</class>
</persistence-unit>
Sabuj Hassan
  • 38,281
  • 14
  • 75
  • 85
  • 1
    Ok, I did this and now my example works. But do I always have to integrate all the classes into the persistence.xml? I thought all was done just by putting the @Entity-Annotation above the class I'd like as a table in the database. Say I now build up a construct of 200 classes, would I have to put them all into the persistence.xml using glasses.myclass? – Socrates Feb 01 '14 at 11:36
  • If you create the classes separately then you have to do it manually. But if you do it using IDE with `create entities from tables` options then the IDE will update the `persistence.xml` file for you. – Sabuj Hassan Feb 01 '14 at 11:41
  • 1
    Alright, it works though. Thanks for helping, Sabuj! – Socrates Feb 01 '14 at 12:04
  • 1
    @SabujHassan You can put in the `persistence.xml` another tag: `false` so that the persistence provider scans all packages finding your annotated classes. Otherwise it would expect you to provide the list of the classes you want it to take into account. You have to consider that scanning all the classes in the class path may cost some delay while initializing persistence unit. – Edwin Dalorzo Feb 01 '14 at 15:00
1

If you don't want to list in the persistence.xml all your entities you can add the following line in the persistence.xml:

<exclude-unlisted-classes>false</exclude-unlisted-classes>
fotis
  • 145
  • 1
  • 7