Note that there are some explanatory texts on larger screens.

plurals
  1. PODisplay attributes of many to many relationship coded with @OneToMany and @ManyToOne
    primarykey
    data
    text
    <p>I'm a newbie in Spring. I think it's better to explain my problem with a little example. Let's say I have two main classes: <code>User</code> and <code>Group</code>. A <code>User</code> can be part of more <code>Group</code>s and a <code>Group</code>, obviously, can have more <code>User</code>s. So the relationship between them is many-to-many. What I would like to show, is something like this (using JSTL):</p> <pre><code>&lt;c:forEach items="${groups}" var="group"&gt; &lt;c:out value="${group.name}"/&gt; (&lt;c:out value="${fn:length(group.users)}" /&gt;):&lt;br /&gt; &lt;c:forEach items="${groups.users}" var="user"&gt; &lt;c:out value="${user.name}"/&gt;&lt;br /&gt; &lt;/c:forEach&gt;&lt;br /&gt; &lt;/c:forEach&gt; </code></pre> <p>Basically, the output should be something like:<br /></p> <blockquote> <p>Random (2):<br /> Joe<br /> Bloggs<br /><br /> Star wars (5):<br /> Luke<br /> Chewbacca<br /> Darth Vader<br /> Princess Leia<br /> Yoda<br /><br /> Nintendo (3):<br /> Super Mario<br /> Metroid<br /> Zelda<br /><br /></p> </blockquote> <p>I initially coded it with the classic <code>@ManyToMany</code> annotation, using an additional table <code>user_has_group</code> (created and managed by JPA) and it was working perfectly.</p> <p>I needed to modify the structure since I needed the <code>user_has_group</code> table to have the <code>joined_date</code> column. To achieve it, I read online that the best solution is to create another class (i.e. <code>UserHasGroup</code>) and add the one-to-many relationships to this class from <code>User</code> and <code>group</code>. Doing so, it's possible to add additional attributes to the <code>UserHasGroup</code> class (and therefore additional columns to the <code>user_has_group</code> table). Something like:</p> <p>User:</p> <pre><code>@Entity @Table(name = "user") public class User { @Id @GeneratedValue @Column private int id; @Column private String alias; @OneToMany(cascade = CascadeType.ALL, mappedBy = "user") private List&lt;UserHasGroup&gt; userHasGroup = new ArrayList&lt;UserHasGroup&gt;(); // Constructors/getters/setters } </code></pre> <p>Group:</p> <pre><code>@Entity @Table(name = "`group`") public class Group { @Id @GeneratedValue @Column private int id; @Column private String name; @OneToMany(cascade = CascadeType.ALL, mappedBy = "group") private List&lt;UserHasGroup&gt; userHasGroup = new ArrayList&lt;UserHasGroup&gt;(); // Constructors/getters/setters } </code></pre> <p>UserHasGroup:</p> <pre><code>@Entity @Table(name = "user_has_group") public class UserHasGroup { @Id @GeneratedValue @Column private int id; @ManyToOne @JoinColumn(name = "user_id") private User user; @ManyToOne @JoinColumn(name = "group_id") private Group group; @Column @Temporal(TemporalType.DATE) private Date joinedDate; // Constructors/getters/setters } </code></pre> <p>So far, so good. All the tests run successfully and the functionality is maintained.</p> <p>But I'm facing a problem with JSTL. In fact, with this new structure is obviously not possible to do <code>group.users</code> to iterate through the users.</p> <p>What is the best way to reach the same functionality as before but with this new structure?</p> <p>Thank you.</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.
 

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