gpt4 book ai didi

java - JPA 异常 : Object: . .. 不是已知的实体类型

转载 作者:搜寻专家 更新时间:2023-10-30 19:52:52 24 4
gpt4 key购买 nike

我是 JPA 的新手,我在自动生成主键值方面遇到了问题。

我有以下实体:

package jpatest.entities;

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

@Entity
public class MyEntity implements Serializable {
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

public Long getId() {
return id;
}

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

private String someProperty;

public String getSomeProperty() {
return someProperty;
}

public void setSomeProperty(String someProperty) {
this.someProperty = someProperty;
}

public MyEntity() {
}

public MyEntity(String someProperty) {
this.someProperty = someProperty;
}

@Override
public String toString() {
return "jpatest.entities.MyEntity[id=" + id + "]";
}
}

以及其他类中的以下主要方法:

public static void main(String[] args) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("JPATestPU");
EntityManager em = emf.createEntityManager();

em.getTransaction().begin();

MyEntity e = new MyEntity("some value");
em.persist(e); /* (exception thrown here) */

em.getTransaction().commit();

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

这是我的持久化单元:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" 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">
<persistence-unit name="JPATestPU" transaction-type="RESOURCE_LOCAL">
<provider>oracle.toplink.essentials.PersistenceProvider</provider>
<class>jpatest.entities.MyEntity</class>
<properties>
<property name="toplink.jdbc.user" value="..."/>
<property name="toplink.jdbc.password" value="..."/>
<property name="toplink.jdbc.url" value="jdbc:mysql://localhost:3306/jpatest"/>
<property name="toplink.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="toplink.ddl-generation" value="create-tables"/>
</properties>
</persistence-unit>
</persistence>

当我执行程序时,在标有正确注释的行中出现以下异常:

Exception in thread "main" java.lang.IllegalArgumentException: Object: jpatest.entities.MyEntity[id=null] is not a known entity type.
at oracle.toplink.essentials.internal.sessions.UnitOfWorkImpl.registerNewObjectForPersist(UnitOfWorkImpl.java:3212)
at oracle.toplink.essentials.internal.ejb.cmp3.base.EntityManagerImpl.persist(EntityManagerImpl.java:205)
at jpatest.Main.main(Main.java:...)

我错过了什么?

最佳答案

我在使用 NetBeans IDE 6.9 时遇到了同样的问题。

显然,这是一个已知问题。看 http://wiki.eclipse.org/EclipseLink/Development/JPA_2.0/metamodel_api#DI_101:_20100218:_Descriptor.javaClass_is_null_on_a_container_EM_for_a_specific_case .另见 http://netbeans.org/bugzilla/show_bug.cgi?id=181068 .

我将下面的最后一行添加到 persistence.xml 并为我修复了它。

<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<!-- Add the following to work around exception issue -->
<exclude-unlisted-classes>false</exclude-unlisted-classes>

关于java - JPA 异常 : Object: . .. 不是已知的实体类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2388755/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com