I have the following class
public class ElementBean {
private String link;
private Set<ElementBean> connections;
}
I need to create a map table where elements are mapped to each other in a many-to-many symmetrical relationship.
@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<ElementBean> getConnections() {
return connections;
}
I have the following requirements
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.
Is this possible?
UPDATE : Thank you for all the suggestions.
When I try @Pascal's suggestion two records are created as follows when I try to connect element 1 with element 2.
FROM_ELEMENT_ID, TO_ELEMENT_ID
1, 2
2, 1
I want the records to be symmetrical where 1,2 is the same as 2,1
How is this issue dealt with?
Update I explicitly created a map bean
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;
}
I updated the ElementBean to
public class ElementBean {
private String link;
private Set<ConnectionBean> from;
private Set<ConnectionBean> to;
}
@OneToMany(mappedBy="from", fetch=FetchType.LAZY)
public Set<ConnectionBean> getFrom() {
return from;
}
@OneToMany(mappedBy="to", fetch=FetchType.LAZY)
public Set<ConnectionBean> getTo() {
return to;
}
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 & B by checking the Connection table for records where
((from == A && to == B) || (from == B && to == A))
before insertion.