简体   繁体   English

如何从JPA中的@EmbeddedId设置反向引用

[英]How to set a backreference from an @EmbeddedId in JPA

Does someone know if it is possible to establish a backreference from within a JPA @EmbeddedId . 有人知道是否可以从JPA @EmbeddedId内部建立反向引用。

So for example there is an Entity of the Form 因此,例如,存在一个实体形式

@Entity
public class Entity1 {
    @Id
    @GeneratedValue
    private String identifier;

    private Entity1 relationToEntity1;
    //Left out the getters and setters for simplicity
}

And a second entity with a complex embedded Id. 第二个实体具有复杂的嵌入式ID。 One part of this second entity is a reference to its parent entity. 第二实体的一部分是对其父实体的引用。 Like so: 像这样:

@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.
}

When I try to save such a construct via JPA (Implementation is EclipseLink) to a database I get several exceptions of the form: 当我尝试通过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)]) 

Did someone encounter such a problem and has a solution? 有人遇到过这样的问题并有解决方案吗?

What you are looking for is a derived Id. 您正在寻找的是一个派生的ID。 If you are using JPA 2.0 then the following will work. 如果您使用的是JPA 2.0,则将可以进行以下操作。 You really do not want the whole Parent to be part of the PK, just the parent's PK. 您确实不希望整个父级都成为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.
}

The @EmbeddedId annotation does not allow relationships in the composite identity class. @EmbeddedId批注不允许组合标识类中的关系。 From the EmbeddedId JavaDoc : EmbeddedId JavaDoc

Relationship mappings defined within an embedded id class are not supported. 不支持在嵌入式id类中定义的关系映射。

I understand that you want Entity2Identifier to contain the key to the parent, but in your example, you're creating a relationship to the entire object rather than just including the primary key of the parent. 我了解您希望Entity2Identifier包含指向父级的键,但是在您的示例中,您正在创建与整个对象的关系,而不仅仅是包含父级的主键。 Even if this construction worked, you would be establishing the composite key to be not only the primary key of the parent, but the entire state of the parent. 即使这种构造有效,您也将建立组合键不仅是父级的主键,而且还应是父级的整个状态。

If you're simply looking for an easy way to establish bi-directional relationships, you can do so with the the @OneToOne annotation and the mappedBy attribute: 如果您只是在寻找一种建立双向关系的简单方法,可以使用@OneToOne批注和mappedBy属性来实现:

@Entity
public class Entity1 {
    @Id
    @GeneratedValue
    private String identifier;

    @OneToOne(mappedBy="relationToEntity1")
    private Entity2 relationToEntity2;
    ...
}

@Entity
public class Entity2 {

    @OneToOne
    private Entity1 relationToEntity1;
    ...
}

With this set of annotations, the JPA provider will handle Entity1.relationToEntity2 and Entity2.relationToEntity1 properly as a bi-directional relationship. 有了这套注释,JPA提供者将处理Entity1.relationToEntity2Entity2.relationToEntity1正确的双向关系。 You might also wish to override the default cascade behavior (none) as well as the default orphan removal behavior (none). 您可能还希望覆盖默认的层叠行为(无)和默认的孤立删除行为(无)。 See the JavaDoc for more details. 有关更多详细信息,请参见JavaDoc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM