Note that there are some explanatory texts on larger screens.

plurals
  1. POSelf referencing symmetrical Hibernate Map Table using @ManyToMany
    primarykey
    data
    text
    <p>I have the following class</p> <pre><code>public class ElementBean { private String link; private Set&lt;ElementBean&gt; connections; } </code></pre> <p>I need to create a map table where elements are mapped to each other in a many-to-many symmetrical relationship. </p> <pre><code>@ManyToMany(targetEntity=ElementBean.class) @JoinTable( name="element_elements", joinColumns=@JoinColumn(name="FROM_ELEMENT_ID", nullable=false), inverseJoinColumns=@JoinColumn(name="TO_ELEMENT_ID", nullable=false) ) public Set&lt;ElementBean&gt; getConnections() { return connections; } </code></pre> <p>I have the following requirements </p> <p>When element A is added as a connection to Element B, then element B should become a connection of Element A. So A.getConnections() should return B and B.getConnections() should return A. I do not want to explicitly create 2 records one for mapping A to B and another for B to A.</p> <p>Is this possible?</p> <p>UPDATE : Thank you for all the suggestions.<br> When I try @Pascal's suggestion two records are created as follows when I try to connect element 1 with element 2. </p> <p>FROM_ELEMENT_ID, TO_ELEMENT_ID<br> 1, 2<br> 2, 1 </p> <p>I want the records to be symmetrical where 1,2 is the same as 2,1 </p> <p>How is this issue dealt with?</p> <p><B>Update</B> I explicitly created a map bean </p> <pre><code>class ConnectionBean { ElementBean from; ElementBean to; } @NotNull @ManyToOne @JoinColumn(name="FROM_ELEMENT_ID", nullable=false) public ElementBean getFrom() { return from; } @NotNull @ManyToOne @JoinColumn(name="TO_ELEMENT_ID", nullable=false) public ElementBean getTo() { return to; } </code></pre> <p>I updated the ElementBean to</p> <pre><code>public class ElementBean { private String link; private Set&lt;ConnectionBean&gt; from; private Set&lt;ConnectionBean&gt; to; } @OneToMany(mappedBy="from", fetch=FetchType.LAZY) public Set&lt;ConnectionBean&gt; getFrom() { return from; } @OneToMany(mappedBy="to", fetch=FetchType.LAZY) public Set&lt;ConnectionBean&gt; getTo() { return to; } </code></pre> <p>Here I can control the insertion and deletion. For example before inserting a new ConnectionBean, I check to see if a connection exists between elements A &amp; B by checking the Connection table for records where<br> ((from == A &amp;&amp; to == B) || (from == B &amp;&amp; to == A))<br> before insertion.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload