答案 0 :(得分:12)
答案 1 :(得分:0)
我也遇到了同样的问题并且遵循了这个答案,但它没有用父实体保存子实体。以下是我所做的更改,并且工作正常。进行以下更改 -
// Child entity's composite primary key class
public class InstanceNotifyEntityId implements Serializable {
@Column(name = "USER_ID")
Long userId;
@JoinColumn(name = "EAIH_ID")
@ManyToOne
InstanceEntity instance
}
// Child entity which contain composite primary key as a EmbeddedId,
// no need to define any relationship here as we already define
// the relationship in composite key class.
@Entity
@Table(name = "EIF_INST_NOTIFIED")
public class InstanceNotifyEntity implements Serializable {
@EmbeddedId
InstanceNotifyEntityId id;
}
// Parent entity (parent entity mappedby should be your composite
// key class instance.child class object which already have the
// join column mapping with "EAID_ID")
@Entity
@Table(name = "EIF_INSTANCE_HDR")
public class InstanceEntity implements Serializable {
@OneToMany(mappedBy = "id.instance,fetch=FetchType.EAGER, cascade = CascadeType.ALL)
private List<InstanceNotifyEntity> userDetails = new ArrayList<InstanceNotifyEntity>();
}
保存父实体时,将父对象设置为复合键,如id.setInstance(parent entire obj)