Hibernate 复合键是另一个表的外键
我有两个表 Part 和 SubPart。零件表具有 id、名称、desc 等通用字段。子零件表具有 part_id、sub_part_id 作为组合键。这两列都引用 Part 表,并且每个列都有一对多映射,就像 Part 表中的每个 part_id 一样,这两列在 SubPart 表中可以有多个条目。我在定义 SubPart 表的复合键时遇到问题。我尝试了嵌入标签,但它不起作用。我该如何解决这个问题。多谢。
像这样的零件表。
@Entity
@Table(name="Part")
public class Part {
@Id
@GeneratedValue
@Column(name="Part_Id")
private int id;
@Column(name="Part_Number")
private String partNumber;
@Column(name="Part_Name")
private String partName;
}
子零件表
@Entity
@Table(name="SubPart")
public class SubPart {
// part and subPart combination is the compound key here.
@ManyToOne
@JoinColumn(name="Part_Id")
private Part part;
@ManyToOne
@JoinColumn(name="Sub_Part_Id")
private Part subPart;
@Column(name="Quantity")
private Integer quantity;
}
I have two table Part and SubPart. Part table has general fields like id, name, desc etc. The SubPart table has part_id, sub_part_id as composite key. Both of these columns are referring to Part table and has a one to many mapping for each of them, like for each part_id in the Part table there can be multiple entries in SubPart table for both the columns. I'm having problem defining the composite key for the SubPart table. I tried the Embedded tag but its not working. How can I address this problem. Thanks a lot.
Part table like this.
@Entity
@Table(name="Part")
public class Part {
@Id
@GeneratedValue
@Column(name="Part_Id")
private int id;
@Column(name="Part_Number")
private String partNumber;
@Column(name="Part_Name")
private String partName;
}
Sub Part Table
@Entity
@Table(name="SubPart")
public class SubPart {
// part and subPart combination is the compound key here.
@ManyToOne
@JoinColumn(name="Part_Id")
private Part part;
@ManyToOne
@JoinColumn(name="Sub_Part_Id")
private Part subPart;
@Column(name="Quantity")
private Integer quantity;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你说
当您有复合主键时,您必须定义一个类(通常是静态内部类)来定义您的复合主键(只是一个建议:因为 Hibernate 使用代理,所以更喜欢将带注释的映射放在 getter 上而不是字段成员上)
注意 Part 和 SubPart 映射已被标记为 insertable=false、updateable=false 因为复合主键中已经定义了映射。 Hibernate不允许您将两个属性映射到同一列,除非您标记 insertable=false、updateable=false。否则你会看到这个很好的异常
You said
When you have a compound primary key, you must define a class (Usually a static inner class) which defines your compound primery key (Just an advice: because Hibernate makes use of proxies, prefer to put your annotated mapping on the getter's instead of field members)
Notice Part and SubPart mapping has been marked as insertable=false, updateable=false Because the mapping has been defined in the compound primary key. Hibernate does not allow you mapping two properties WITH THE SAME COLUMN unless you mark insertable=false, updateable=false. Otherwise you will see this nice exception
我想我会声明一个 Map类型的字段在类 Part 中并将其声明为 @OneToMany。
实际上,在这种特殊情况下,它可能是一个 Map。因为唯一的其他字段是数量。不需要 SubPart 类。
I think I would declare a field of type Map<Part,SubPart> in class Part and declare it as @OneToMany.
Actually, in this particular case, it could event be a Map<Part,Integer> because the only other field is the quantity. The SubPart class is not needed.