我有两个具有各自接口的类,我想在它们之间创建 JPA@OneToOne
关系。这失败了[class EmployeeImpl] uses a non-entity [class Adress] as target entity in the relationship attribute [field adress]
。
第一个接口/类:
public interface Employee {
public long getId();
public Adress getAdress();
public void setAdress(Adress adress);
}
@Entity(name = "EmployeeImpl")
@Table(name = "EmployeeImpl")
public class EmployeeImpl implements Employee {
@Id
@Column(name = "employeeId")
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@OneToOne(cascade = CascadeType.PERSIST)
private Adress adress;
// snip, getters and setters
}
第二个接口/类:
public interface Adress {
public long getId();
public String getStreet();
public void setStreet(String street);
}
@Entity(name = "AdressImpl")
@Table(name = "AdressImpl")
public class AdressImpl implements Adress {
@Id
@Column(name = "AdressId")
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@Column(name = "Street")
private String street;
// Snip getters and setters
}
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="employee"
transaction-type="RESOURCE_LOCAL">
<class>EmployeeImpl</class>
<class>AdressImpl</class>
<properties>
<property name="eclipselink.create-ddl-jdbc-file-name"
value="create-matterhorn-employee.jdbc" />
<property name="eclipselink.drop-ddl-jdbc-file-name"
value="drop-matterhorn-employee.jdbc" />
</properties>
</persistence-unit>
</persistence>
我缩短了包名称和导入等。尝试创建 EntityManagerFactory(您在其中移交持久性单元)时发生异常。我正在使用 Eclipse 链接 2.0.2。