简体   繁体   English

了解休眠中的多对多映射

[英]Understanding many to many mapping in hibernate

I am referring to the example at - http://viralpatel.net/blogs/hibernate-many-to-many-annotation-mapping-tutorial/ Employees can attend many Meetings and Meetings can be attended by many Employees. 我指的是以下示例: http://viralpatel.net/blogs/hibernate-many-to-many-annotation-mapping-tutorial/ 员工可以参加很多会议,而很多员工也可以参加会议

Let the owning class be Employee. 让拥有类为Employee。 The many to many mapping code inside Employee will be - Employee内部的多对多映射代码将是-

@ManyToMany(cascade = {CascadeType.ALL})  
@JoinTable(name="EMPLOYEE_MEETING",   
            joinColumns={@JoinColumn(name="EMPLOYEE_ID")},   
            inverseJoinColumns={@JoinColumn(name="MEETING_ID")})  
private Set<Meeting> meetings = new HashSet<Meeting>();  

I looked at several other examples and documentation, but I am still not sure how this works. 我查看了其他几个示例和文档,但是我仍然不确定这是如何工作的。 I don't think I fully understand what joinColumns and inverseJoinColumns do. 我认为我不完全了解joinColumns和inverseJoinColumns的作用。

joinColumns={@JoinColumn(name="EMPLOYEE_ID")} - Does this line only tell hibernate that Employee id of employee joins employee id of the mapping table ? joinColumns={@JoinColumn(name="EMPLOYEE_ID")} -此行是否仅告诉休眠状态,员工的员工ID已加入映射表的员工ID?

inverseJoinColumns={@JoinColumn(name="MEETING_ID")}) ? inverseJoinColumns={@JoinColumn(name="MEETING_ID")})吗? Does this line only tell hibernate to join Employee id to the Meeting_Id column in the link table ? 此行是否仅告诉hibernate将Employee ID加入到链接表中的Meeting_Id列?

What the actual annotation is saying is that Hibernate should create an intermediate join table. 实际的注释是说Hibernate应该创建一个中间联接表。 One column of this table should map to the column on your Employee table called Employee_ID (that's your joinColumns parameter), and the other column should map to the column on your Meeting table called Meeting_ID (that's your inverseJoinColumns parameter). 该表的一列应映射到Employee表上名为Employee_ID的列(这是您的joinColumns参数),另一列应映射到您的Meeting表上称为Meeting_ID的列(即您的inverseJoinColumns参数)。

The joinColumns parameter should specify the PK of the owning entity (in this case your Employee object), while the inverseJoinColumn specifies the PK of the non-owning entity. joinColumns参数应指定拥有实体的PK(在本例中为您的Employee对象),而inverseJoinColumn参数指定非拥有实体的PK。

Note that you don't always have to just specify your join columns as PK's, but it makes sense because uniqueness is enforced and its kind of the point of a PK. 请注意,您不必总是只将连接列指定为PK,但这是有道理的,因为强制执行了唯一性及其对PK的意义。 You can also have multiple join columns forming a composite key on either side of the relationship within the join table. 您还可以在联接表中的关系的任一侧具有多个联接列,以形成一个复合键。

You seem to have a handle on how the relationships are set up but I'll say this for the benefit of people coming here from Google. 您似乎对建立关系的方式有所了解,但是我要说这是为了让来自Google的人们受益。 In a bidirectional relationship you have to specify which side of the relationship 'owns' the relationship. 在双向关系中,您必须指定关系的哪一方“拥有”该关系。 While you can have a bidirectional cascade as well, you usually only access the non-owning object through the owning one. 虽然也可以具有双向级联,但是通常只能通过拥有的对象访问非拥有的对象。 So you don't generally access a Meeting object directly, you access it through a Employee object. 因此,通常您不直接访问Meeting对象,而是通过Employee对象访问它。 This becomes more important in a uni-directional relationship but it can cause a few problems in a bidirectional relationship in determining what you want to cascade. 这在单向关系中变得尤为重要,但在确定要级联的内容时,可能会在双向关系中引起一些问题。

For example, if you decide that if you delete a Meeting , you don't want the deletion to cascade to the join table, you need to change your CascadeType in your Meeting class. 例如,如果您决定如果删除Meeting ,则不希望该删除级联到CascadeType表,则需要在Meeting类中更改CascadeType

If you want me to expand on my explanation please let me know. 如果您想让我进一步解释,请告诉我。

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

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