Note that there are some explanatory texts on larger screens.

plurals
  1. POHibernate @OneToMany relationship mapping
    primarykey
    data
    text
    <p>I am trying to design some kind of user to user relationship, such as "user A follows user B" and "User A wants to be User B's friend".</p> <p>I have a User class, and the way it is designed looks like this:</p> <pre><code>@Entity public class User{ @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER) List&lt;User&gt; followers; @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER) List&lt;User&gt; following; @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER) List&lt;User&gt; friendRequests; @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER) List&lt;User&gt; requesting; @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER) List&lt;User&gt; friends; } </code></pre> <p>I am running into two problems:</p> <ul> <li>Hibernate is giving me <strong>cannot simultaneously fetch multiple bags</strong> problem</li> <li>I have looked up online, people said to remove FetchType.EAGER or change it to Set instead of List, but that resulted me <strong>Field doesn't have a default value</strong></li> </ul> <p>I have a feeling that the relationship is not defined properly, and also I should be seeing more tables, because right now, I only see User table, and User_User table.</p> <p><hr/> <strong>Update</strong></p> <p>The following creates 3 table, friends, followers, and requesters. Is this somewhat optimized compared to 5 tables? And are there any advantage to this in comparison with what Mr.J4mes suggested?</p> <pre><code>@ManyToMany @JoinTable(name = "followers", joinColumns = @JoinColumn(name = "followerId"), inverseJoinColumns = @JoinColumn(name = "userId")) private List&lt;User&gt; followers; @ManyToMany @JoinTable(name = "followers", joinColumns = @JoinColumn(name = "userId"), inverseJoinColumns = @JoinColumn(name = "followerId")) private List&lt;User&gt; following; @ManyToMany @JoinTable(name = "friends", joinColumns = @JoinColumn(name = "userId"), inverseJoinColumns = @JoinColumn(name = "friendId")) private List&lt;User&gt; friends; @ManyToMany @JoinTable(name = "requesters", joinColumns = @JoinColumn(name = "requesterId"), inverseJoinColumns = @JoinColumn(name = "userId")) private List&lt;User&gt; friendRequests; @ManyToMany @JoinTable(name = "requesters", joinColumns = @JoinColumn(name = "userId"), inverseJoinColumns = @JoinColumn(name = "requesterId")) private List&lt;User&gt; requesting; </code></pre>
    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