Java JPA 异常:对象:...不是已知的实体类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2388755/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JPA exception: Object: ... is not a known entity type
提问by mmutilva
I'm new to JPA and I'm having problems with the autogeneration of primary key values.
我是 JPA 的新手,我在自动生成主键值时遇到了问题。
I have the following entity:
我有以下实体:
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 + "]";
}
}
and the following main method in other class:
以及其他类中的以下主要方法:
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();
}
This is my persistence unit:
这是我的持久化单元:
<?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>
When I execute the program I get the following exception in the line marked with the proper comment:
当我执行程序时,在标有正确注释的行中出现以下异常:
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:...)
What am I missing?
我错过了什么?
采纳答案by Lars Tackmann
TopLink used to require you to explicitly set GenerationType.IDENTITYfor MySQL, so change this and drop the database. Then try running your sample again. Further you might also want to explcitly set the database platform:
TopLink 过去要求您为 MySQL显式设置GenerationType.IDENTITY,因此更改此设置并删除数据库。然后再次尝试运行您的示例。此外,您可能还想明确设置数据库平台:
<property name="toplink.platform.class.name"
value="oracle.toplink.platform.database.MySQL4Platform"/>
Also I vaguely remember that you have to run Toplink using its Java agent in order to make it function properly with a resource local entitymanager.
此外,我依稀记得您必须使用其 Java 代理运行 Toplink,才能使其与资源本地实体管理器一起正常运行。
I did however successfully run your example using EclipseLink (which you should use since Toplink is outdated). Only cavat was that I did not have MySQL server handy, so I ran it using H2. I used the following Maven pom.xml to resolve the dependencies:
但是,我确实使用 EclipseLink 成功运行了您的示例(您应该使用它,因为 Toplink 已过时)。唯一的遗憾是我手头没有 MySQL 服务器,所以我使用 H2 运行它。我使用以下 Maven pom.xml 来解决依赖关系:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.randompage</groupId>
<artifactId>sandbox</artifactId>
<packaging>jar</packaging>
<version>1.0</version>
<name>sandbox</name>
<repositories>
<repository>
<id>EclipseLink Repo</id>
<url>http://www.eclipse.org/downloads/download.php?r=1&nf=1&file=/rt/eclipselink/maven.repo</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence</artifactId>
<version>2.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>2.0.0</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.2.130</version>
</dependency>
</dependencies>
</project>
and this persistence.xml:
和这个persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.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_2_0.xsd">
<persistence-unit name="JPATestPU" transaction-type="RESOURCE_LOCAL">
<provider>
org.eclipse.persistence.jpa.PersistenceProvider
</provider>
<class>org.randompage.MyEntity</class>
<properties>
<property name="javax.persistence.jdbc.user" value="johndoe"/>
<property name="javax.persistence.jdbc.password" value="secret"/>
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:h2:~/.h2/testdb;FILE_LOCK=NO"/>
<property name="eclipselink.ddl-generation" value="create-tables"/>
<property name="eclipselink.logging.level" value="INFO"/>
</properties>
</persistence-unit>
</persistence>
With these settings your code ran as expected.
使用这些设置,您的代码按预期运行。
回答by Zak
I use this syntax rather than type AUTO
我使用这种语法而不是键入 AUTO
@javax.persistence.Id
@javax.persistence.GeneratedValue(strategy = GenerationType.IDENTITY)
Then, I use the simple type "long" for ID's with a lowercase l :
然后,我使用简单类型“long”作为带有小写 l 的 ID:
private long taskID;
This may be unrelated, but I also specify a different table name for my entities:
这可能无关,但我还为我的实体指定了不同的表名:
@javax.persistence.Entity(name = "Tasks")
public class Task implements Serializable
回答by bert
You could try and leave the definition out of the persistnce.xml The Persistence provider should than scan all classes in the classpath for @Entity annotations.
您可以尝试将定义保留在 persistence.xml 之外 Persistence 提供程序应该扫描类路径中的所有类以获取 @Entity 注释。
回答by Zak
I also have to add one other item to my persistence.xml when changing class/table defs so that the EM knows to build/update tables:
我还必须在更改类/表定义时向我的 persistence.xml 添加另一项,以便 EM 知道构建/更新表:
<property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(SchemaAction='refresh')"/>
If I want a fresh start, I instead use:
如果我想重新开始,我会使用:
<!--<property name="openjpa.jdbc.SynchronizeMappings"
value="buildSchema(SchemaAction='dropDB,add')"/>
-->
I noticed that in your persistence.xml schema management is only set to "create tables" as opposed to drop/create, or update
我注意到在您的 persistence.xml 模式管理中仅设置为“创建表”,而不是删除/创建或更新
回答by Moi Lejter
I ran into the same exception, when deploying web applications to GlassFish v3 (which uses EclipseLink as its JPA provider). I am not sure it's the same scenario as above - but the explanation for this bug in my case might help others :-) - turns out there's a bug in EclipseLink, when running under OSGi (which is the case in GlassFish), which leads EclipseLink to hold on to an "old" version of the entity class when re-deploying, resulting in this exception. The bug report is here.
在将 Web 应用程序部署到 GlassFish v3(使用 EclipseLink 作为其 JPA 提供程序)时,我遇到了同样的异常。我不确定它是否与上述情况相同 - 但在我的案例中对这个错误的解释可能对其他人有帮助:-) - 结果是 EclipseLink 中存在一个错误,当在 OSGi 下运行时(在 GlassFish 中就是这种情况),这导致EclipseLink 在重新部署时保留实体类的“旧”版本,从而导致此异常。错误报告在这里。
回答by Aardvocate Akintayo Olusegun
As far as I know, whenever I get this error, I just re-start glassfish. Works everytime.
据我所知,每当出现此错误时,我都会重新启动 glassfish。每次都有效。
回答by Charles
I ran into this same problem using NetBeans IDE 6.9.
我在使用 NetBeans IDE 6.9 时遇到了同样的问题。
Apparently, this is a known issue. See 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. Also see http://netbeans.org/bugzilla/show_bug.cgi?id=181068.
显然,这是一个已知问题。请参阅 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。
I added the last line below to persistence.xml and it fixed it for me.
我将下面的最后一行添加到persistence.xml 并为我修复了它。
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<!-- Add the following to work around exception issue -->
<exclude-unlisted-classes>false</exclude-unlisted-classes>
回答by Alberto
As Charlespointed out in his answer, the problem is not the id generation, but the persistence layer not finding the entity.
正如查尔斯在他的回答中指出的那样,问题不在于 id 生成,而在于持久层没有找到实体。
As you, I am also new to JPA. I have tried to write a "Hello World" JPA application using org.eclipse.persistence.jpa.PersistenceProvider
when I got this error. The mentioned workaround also worked for me. Moreover, through trial-error I also found that to declare your entities, you must always anotate @entity
in each entityand:
和你一样,我也是 JPA 的新手。org.eclipse.persistence.jpa.PersistenceProvider
当我收到此错误时,我尝试编写一个“Hello World”JPA 应用程序。提到的解决方法也对我有用。此外,通过反复试验,我还发现要声明您的实体,您必须始终@entity
在每个实体中进行注释,并且:
- if you set
exclude-unlisted-classes
totrue
, you also have to list the entities withinclass
elements in yourpersistence.xml
- if you set
exclude-unlisted-classes
tofalse
the persistence layer can find the entitiesregardles of theclass
element in yourpersistence.xml
.
- 如果设置
exclude-unlisted-classes
为true
,则还必须列出class
元素中的实体persistence.xml
- 如果设置
exclude-unlisted-classes
到false
持久层可以找到实体的的regardlesclass
元素在你的persistence.xml
。
回答by John John Pichler
Check the class output folder of eclipse, sometimes you change the xml and it was not updated.
检查eclipse的class output文件夹,有时你更改了xml并没有更新。
回答by Kalpesh Soni
if you are only getting this error in junit
如果您只在 junit 中收到此错误
try adding this in persistence.xml
尝试在persistence.xml中添加这个
<jar-file>file:../classes</jar-file>