jpa hibernate复合外键映射 [英] jpa hibernate composite foreign key mapping
问题描述
我无法为某些实体设置jpa映射。
I am having trouble setting up jpa mappings for some entities. I have a parent entity defined like the following.
@Entity
@Table(name="EIF_INSTANCE_HDR")
public class InstanceEntity implements Serializable{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(generator="eif_inst_gen")
@SequenceGenerator(name="eif_inst_gen",sequenceName="EIF_INSTANCE_SEQ")
@Column(name = "EAIH_ID")
private Long eaihid;
@Column(name = "EAD_ID")
private Long eadid;
@OneToMany(targetEntity=InstanceNotifyEntity.class, mappedBy="instance",fetch=FetchType.EAGER, cascade = CascadeType.ALL)
private List<InstanceNotifyEntity> userDetails = new ArrayList<InstanceNotifyEntity>();
}
然后我有一个子实体w / a组合键和一个外键到这个表的主键如下:
I then have a child entity w/ a composite key, and a foreign key to the primary key of this table as follows:
@Entity
@Table(name="EIF_INST_NOTIFIED")
public class InstanceNotifyEntity implements Serializable{
private static final long serialVersionUID = 1L;
@Id
@ManyToOne
@JoinColumn(name="EAIH_ID", referencedColumnName="EAIH_ID")
private InstanceEntity instance;
@Id
@Column(name="USER_ID")
private Long userId;
@Column(name="COMMENT_TXT")
private String commentText;
}
我知道子实体是不正确的,但我不确定如何设置最多有一个复合PK。我知道我需要设置一个PK类,但我不知道如何做到这一点,当一个字段是父类的外键。一旦设置好,父母如何引用孩子实体?
I know the child entity is incorrect, but I am unsure how to set this up to have a composite PK. I know I need to setup a PK class, but I am not sure how to do that when one field is a foreign key to the parent class. And once that is setup how would the parent reference the child entity?
任何帮助都是值得赞赏的。 解决方案
Any help is appreciated.
这是由 JPA 2规范
a>部分2.4.1,与派生身份对应的主键。该部分包含两个直接适用于您的问题的示例。
This is governed by JPA 2 spec section 2.4.1, "Primary Keys Corresponding to Derived Identities". The section contains two examples directly applicable to your problem.
正如规范中所述,在这种情况下,有两种方法可以表示子实体的键:
As described in the spec, there are two ways to represent the child entity's key in this case:
-
@IdClass
-
@EmbeddedId
@IdClass
@EmbeddedId
以下是 EmbeddedId $ c $方法。我任意选择了
EmbeddedId
,但是 IdClass
和 EmbeddedId
是重要的。您可以选择不同的方式。
Here's a rough sketch of the EmbeddedId
way. I chose EmbeddedId
arbitrarily, but the choice between IdClass
and EmbeddedId
is significant. You might choose differently.
// Child entity's composite primary key
@Embeddable
public class InstanceNotifyEntityId implements Serializable {
Long eaihId;
Long userId;
}
// Child entity
@Entity
@Table(name="EIF_INST_NOTIFIED")
public class InstanceNotifyEntity implements Serializable {
@AttributeOverrides({
@AttributeOverride(name="userId", column = @Column(name="USER_ID"))
@AttributeOverride(name="eaihId", column = @Column(name="EAIH_ID"))
})
@EmbeddedId
InstanceNotifyEntityId id;
@MapsId("eaihId")
@ManyToOne
InstanceEntity instance;
// ...
}
父实体需要改变: userDetails
属性 mappedBy
应该是id.eaihId。我认为就是这样,但我之前没有使用过像这样的实体。可能错过了一些东西......如果您发现错误,请发帖。
The parent entity needs one change: the userDetails
attribute mappedBy
should be "id.eaihId". I think that's it, but I haven't used entities exactly like this before. Might have missed something... please post if you see errors.
这篇关于jpa hibernate复合外键映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!