NetBeans11.2-JPA-java.lang.IllegalArgumentException:对象:javaapplication4.Id [id = 5]不是已知的实体类型

时间:2019-11-05 22:58:32

标签: jpa eclipselink derby persistence.xml netbeans-11

我一遍又一遍地在MacOS的NetBeans 11.2中尝试使用JPA时遇到以下异常。

  

java.lang.IllegalArgumentException:对象:javaapplication4.Id [id = 5]不是已知的实体类型。

     

在org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.registerNewObjectForPersist(UnitOfWorkImpl.java:4228)

     

在org.eclipse.persistence.internal.jpa.EntityManagerImpl.persist(EntityManagerImpl.java:496)

     

在javaapplication4.JavaApplication4.main(JavaApplication4.java:30)

我的主班:

package javaapplication4;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;

/**
 *
 * @author Informatica
 */
public class JavaApplication4 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        EntityManagerFactory emf = Persistence.createEntityManagerFactory("JavaApplication4PU");
        EntityManager em = emf.createEntityManager();
        EntityTransaction tx = em.getTransaction();
        tx.begin();
        try{
            Id id = new Id();
            em.persist(id);
            tx.commit();

        }catch(Exception e ){
            e.printStackTrace();
            tx.rollback();
        }


        em.close();
        emf.close();
    }

}

我的实体类:

package javaapplication4;

import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

/**
 *
 * @author Informatica
 */
@Entity
@Table(name = "ID")
@NamedQueries({
    @NamedQuery(name = "Id.findAll", query = "SELECT i FROM Id i"),
    @NamedQuery(name = "Id.findById", query = "SELECT i FROM Id i WHERE i.id = :id")})
public class Id implements Serializable {

    private static final long serialVersionUID = 1L;
    @javax.persistence.Id
    @Column(name = "ID")
    private Long id;

    public Id() {
        this.id = new Long(5);
    }

    public Id(Long id) {
        this.id = id;
    }

    public Long getId() {
        return id;
    }

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

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Id)) {
            return false;
        }
        Id other = (Id) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "javaapplication4.Id[ id=" + id + " ]";
    }

}

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="JavaApplication4PU" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <class>javaapplication4.Id</class>
    <properties>
      <property name="javax.persistence.jdbc.url" value="jdbc:derby://localhost:1527/s"/>
      <property name="javax.persistence.jdbc.user" value="s"/>
      <property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver"/>
      <property name="javax.persistence.jdbc.password" value="s"/>
      <property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
    </properties>
  </persistence-unit>
</persistence>

我的图书馆:

libraries

classpath

我正在使用Derby DB。

让我知道您是否可以找到问题所在。预先谢谢你。

1 个答案:

答案 0 :(得分:0)

问题已解决,由于某种原因,它仅适用于jdk 8,切换jkd解决了该问题