有人知道是否可以在 JPA 内建立反向引用@EmbeddedId
.
例如,有一个形式的实体
@Entity
public class Entity1 {
@Id
@GeneratedValue
private String identifier;
private Entity1 relationToEntity1;
//Left out the getters and setters for simplicity
}
还有一个具有复杂嵌入 ID 的第二个实体。第二个实体的一部分是对其父实体的引用。就像这样:
@Entity
public class Entity2 {
@EmbeddedId private Entity2Identifier id;
//Left out the getters and setters for simplicity.
}
@Embedabble
public class Entity2Identifier {
private String firstPartOfIdentifier;
private Entity1 parent;
//Left out the getters and setters for simplicity.
}
当我尝试通过 JPA(实现是 EclipseLink)将这样的构造保存到数据库时,我收到以下形式的几个异常:
Exception [EclipseLink-93] (Eclipse Persistence Services - 1.1.0.r3639-SNAPSHOT):
org.eclipse.persistence.exceptions.DescriptorException
Exception Description: The table [ENTITY1] is not present in this descriptor.
Descriptor: RelationalDescriptor(test.Entity2 --> [DatabaseTable(ENTITY2)])
您正在寻找的是派生 ID。如果您使用 JPA 2.0,则以下内容将起作用。您确实不希望整个父级成为 PK 的一部分,而只是父级 PK 的一部分。
@Entity
public class Entity1 {
@EmbeddedId
private ParentId identifier;
@OneToOne(mappedBy="relationToEntity1")
private Entity2 relationToEntity2;
//Left out the getters and setters for simplicity
}
@Entity
public class Entity2 {
@EmbeddedId private Entity2Identifier id;
//Left out the getters and setters for simplicity.
@MapsId("parentId")
@OneToOne
private Entity1 parent;
}
@Embedabble
public class Entity2Identifier {
private String firstPartOfIdentifier;
private ParentId parentId;
//Left out the getters and setters for simplicity.
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)