[英]JPA embeddable entity with composite id
I have a rather simple schema, I think, but I can't find the right way of mapping it. 我认为我有一个相当简单的模式,但是我找不到映射它的正确方法。
@Entity
class Satellite {
@Id
private int id;
// stuff
@ElementCollection @OrderBy(value = "orbit asc")
private List<DataModel> dataModel;
}
the embeddable class is the following: 可嵌入类如下:
class DataModel {
private int orbit;
private int data;
}
the problem is, orbit should be unique for each satellite. 问题是,每个卫星的轨道应该是唯一的。 In my mind the table representing datamodel has a composite primary key composed of the satellite id and orbit. 在我看来,代表数据模型的表格具有由卫星ID和轨道组成的复合主键。 But I can't find the right way of mapping it. 但是我找不到正确的映射方式。 If I declare the DataModel as a plain entity, I have to add a satellite field mapped manytoone and a composite primary key that include satellite and orbit, but it does not works (stack overflow!!). 如果我将DataModel声明为简单实体,则必须添加一个映射了mantoone的卫星字段和一个包含卫星和轨道的复合主键,但是它不起作用(堆栈溢出!)。
I'm using hibernate as persistence provider. 我正在使用休眠作为持久性提供程序。
If I declare the DataModel as a plain entity, I have to add a satellite field mapped manytoone and a composite primary key that include satellite and orbit. 如果我将DataModel声明为简单实体,则必须添加一个映射了mantoone的卫星字段和一个包含卫星和轨道的复合主键。
On the db level you'd have to do that anyways, ie the table containing the DataModel
would need a foreign key to the satellite. 在数据库级别,无论如何都必须执行此操作,即包含DataModel
的表将需要卫星的外键。 In that case I don't see a reason not to make DataModel
an entity: 在那种情况下,我看不出不使DataModel
成为实体的原因:
@IdClass( DMKeyClass.class )
@Entity
class DataModel {
@Id
private Satellite sat;
@Id
private int orbit;
private int data;
}
class DMKeyClass {
public int sat;
public int orbit;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.