我创建了一个 JPA
项目。在那个 Eclipse 中,在实体类上显示以下错误。
Class "model.Address" is listed in the
persistence.xml
file but not mapped
我应该如何映射 persistance.xml
中的实体类?
这里是 model.Address
实体:
package model;
import java.io.Serializable;
import javax.persistence.*;
@Entity
public class Address implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private long id;
private String city;
private String country;
private String province;
private String postalCode;
private String street;
// Getters/setters omitted for brevity.
}
这里是persistence.xml
:
<?xml version="1.0" encoding="UTF-8" ?>
<persistence
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"
version="2.0"
>
<provider>org.eclipse.persistance.example.jpa.20.employee.annotations</provider>
<persistence-unit name="employee" transaction-type="RESOURCE_LOCAL">
<class>model.Employee</class>
<class>model.Address</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="oracle.jdbc.OracleDriver" />
<property name="javax.persistence.jdbc.url" value="jdbc:oracle:thin:@localhost:1521:orcl" />
<property name="javax.persistence.jdbc.user" value="scott" />
<property name="javax.persistence.jdbc.password" value="tiger" />
</properties>
</persistence-unit>
</persistence>
最佳答案
这是 Eclipse 的一个怪癖。我最近在创建一个禁用 JPA 库配置的新 JPA 项目时遇到了这个问题,但没有手动配置 JPA 库之前我通过 Eclipse 创建实体 New JPA 实体 向导。创建实体后,我在项目的 Build Path 中配置了 JPA 库(只需在 Libraries 中添加目标 Java EE 服务器运行时),但验证错误仍然存在。我可以通过以下至少一种方式解决它:
- 右键
persistence.xml
文件,JPA工具 -> 同步类列表。 - 或者,右键单击项目,验证。
- 或者,关闭/重新打开项目。
这是始终可重复的。我使用的是 Eclipse Indigo SR1。当我在配置 JPA 库之后创建实体时,不会发生此验证错误。
关于eclipse - 类 "model.Address"在 persistence.xml 文件中列出但未映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2242637/