Question

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?

Was it helpful?

Solution

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>

OTHER TIPS

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>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top