我试图让 javax.persistence 运行,但出现错误。 我建立了一个用于测试的小项目,创建了一个实体类、persistence.xml 和运行过程:
实体类:
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;
}
}
持久性.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>
错误消息:
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
有人知道问题所在吗?洪德级有什么问题吗?或者在 persistence.xml 中?
最佳答案
在 persistence.xml
中使用 Hund 类,如下例所示。只需将其放在属性和持久性单元标记之间即可。
</properties>
<class>glasses.Hund</class>
</persistence-unit>
关于java - 使用 JPA 构建 Java 项目时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21497089/