user1883212
user1883212

Reputation: 7859

Join Table and OneToMany relation in Hibernate

I created two entities for the tables HOBBY and STUDENT, but I have difficulties to retrieve the information I need from the Join Table.

This is the schema:

STUDENT              STUDENT_HOBBY        HOBBY
--------             -----------          --------
id ------------------ student_id
name                  hobby_id ---------- id
lastname                                  hobby_name    
---------             ----------          --------

where
Student to student_hobby = one to many
Hobby to student_hobby = one to many
(that means a student may have many hobbies and a hobby can belong to more than one student)

This is the Student entity class:

@Entity
@Table(name="STUDENT")
public class Student {

    @OneToMany
    @JoinTable(name="student_hobbies", joinColumns=@JoinColumn(name="student_id"),
       inverseJoinColumns=@JoinColumn(name="hobby_id"))
    private Collection<Hobby> hobbies;

    @Id
    @Column(name="ID")
    @GeneratedValue
    private Integer id;

    @Column(name="NAME")
    private String name;

    @Column(name="LASTNAME")
    private String lastName;

    // Getters and setters here
}

This is the Hobby entity class:

@Entity
@Table(name="HOBBY")
public class Hobby {

    @OneToMany
    @JoinTable(name="student_hobbies", joinColumns=@JoinColumn(name="hobby_id"),
       inverseJoinColumns=@JoinColumn(name="student_id"))
    private Collection<Student> students;

    @Id
    @Column(name="ID")
    @GeneratedValue
    private Integer id;

    @Column(name="HOBBY_NAME")
    private String hobby_name;

    // Getters and setters here
}

Now I would like to implement the following DAO, but I don't know exactly how to do:

public interface MyDAO {
    public void addHobbyForStudent(int student_id, int hobby_id);   
    public void RemoveHobbyForStudent(int student_id, int hobby_id);
}

Should I create an other entity class for the Join Table? Could someone give me some indication on the way to follow?

Upvotes: 2

Views: 1515

Answers (1)

Daniel Kaplan
Daniel Kaplan

Reputation: 67514

You can create an entity class for the join table, but you don't need to and you probably shouldn't. This will just create more code to maintain. You should create an entity for the join table if there is data in the join table you need to query. EG: if there was a start_time or something in the join table and you wanted to be able to see the start_time.

You should be using the @ManyToMany annotation when you're avoiding a join table entity. The documentation I linked to gives some good examples of how to do this:

Example 1:

// In Customer class:

@ManyToMany
@JoinTable(name="CUST_PHONES")
public Set<PhoneNumber> getPhones() { return phones; }

// In PhoneNumber class:

@ManyToMany(mappedBy="phones")
public Set<Customer> getCustomers() { return customers; }

Example 2:

// In Customer class:

@ManyToMany(targetEntity=com.example.PhoneNumber.class)
public Set getPhones() { return phones; }

// In PhoneNumber class:

@ManyToMany(targetEntity=com.example.Customer.class, mappedBy="phones")
public Set getCustomers() { return customers; }

Example 3:

// In Customer class:

@ManyToMany
@JoinTable(name="CUST_PHONE",
    joinColumns=
        @JoinColumn(name="CUST_ID", referencedColumnName="ID"),
    inverseJoinColumns=
        @JoinColumn(name="PHONE_ID", referencedColumnName="ID")
    )
public Set<PhoneNumber> getPhones() { return phones; }

// In PhoneNumberClass:

@ManyToMany(mappedBy="phones")
public Set<Customer> getCustomers() { return customers; }

Upvotes: 2

Related Questions