tags:

views:

259

answers:

3

I'm building a web application using Spring framework and Hibernate with annotation and get stuck with a simple mapping between two entities.

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:

User class:

@Entity
@Table(name="USER")
public class User {

    private Long id;
    private String email;
    private String password;
    private Set courses = new HashSet(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 getCourses() {
        return courses;
    }

    public void setCourses(Set courses) {
        this.courses = courses;
    } 
}

Course class:

@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;
    }

}

The problem is that this implementation only allows me to go one way

 user.getCourses() 

What do I need to change, so I can go in both directions?

 user.getCourses() 
course.getUsers()

Any help would be appreciated.

A: 

Hibernate isn't going to magically add a getUsers() method to your Course class, you need to do that yourself, along with a Set<User> field.

I don't know which tutorial you read, but you should read proper documentation (see the section of the manual on bidirectional associations), in particular where it talks about many-to-many relations.

skaffman
A: 

i guess what you need is a bidirectional manyTomany relationship. In that case one side has to be the owner. This example may help: http://docs.jboss.org/hibernate/stable/annotations/reference/en/html_single/ (2.2.5.3.3. Many-to-many)

kukudas
A: 

One thing to try is to use the @MappedBy annotation for the side of the relationship that DOESN'T own the relationship.

ONe other point, if there no reason why you explicitly need to add all of the other annotations (like @Table, @Column) you can leave them out and let Hibernate do this work for you behind the scenes. The hibernate team recommend using the defaults (by not putting in the annotations) unless you need to.

darren