Note that there are some explanatory texts on larger screens.

plurals
  1. POhibernate annotation bi-directional mapping
    primarykey
    data
    text
    <p>I'm building a web application using Spring framework and Hibernate with annotation and get stuck with a simple mapping between two entities.</p> <p>I'm trying to create a many-to-many relationship between User and Course. I followed one of the Hibernate tutorials and my implementation is as follows:</p> <p>User class:</p> <pre><code>@Entity @Table(name="USER") public class User { private Long id; private String email; private String password; private Set&lt;Course&gt; courses = new HashSet&lt;Course&gt;(0); @Id @GeneratedValue @Column(name="USER_ID") public Long getId() { return id; } public void setId(Long id) { this.id = id; } @Column(name="USER_EMAIL") public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } @Column(name="USER_PASSWORD") public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @ManyToMany(cascade = CascadeType.ALL) @JoinTable(name = "USER_COURSE", joinColumns = { @JoinColumn(name = "USER_ID") }, inverseJoinColumns = { @JoinColumn(name = "COURSE_ID") }) public Set&lt;Course&gt; getCourses() { return courses; } public void setCourses(Set&lt;Course&gt; courses) { this.courses = courses; } } </code></pre> <p>Course class:</p> <pre><code>@Entity @Table(name="COURSE") public class Course { private Long id; private String name; @Id @GeneratedValue @Column(name="COURSE_ID") public Long getId() { return id; } public void setId(Long id) { this.id = id; } @Column(name="NAME") public String getName() { return name; } public void setName(String name) { this.name = name; } } </code></pre> <p>The problem is that this implementation only allows me to go one way</p> <pre><code>user.getCourses() </code></pre> <p>What do I need to change, so I can go in both directions?</p> <pre><code>user.getCourses() course.getUsers() </code></pre> <p>Any help would be appreciated.</p>
    singulars
    1. This table or related slice is empty.
    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