0

我一遍又一遍地尝试在 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 + " ]";
    }

}

持久性.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>

我的图书馆:

图书馆

类路径

我正在使用 Derby DB。

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

4

1 回答 1

0

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

于 2019-11-06T13:16:19.167 回答