Filip
Filip

Reputation: 2344

Hibernate - many to many - with table of constants

I am using standard Hibernate way of many to many relation. For instance, Employee with Meetings as follows:

 @Entity
    @Table(name="EMPLOYEE")
    public class Employee {

    @Id
    @Column(name="EMPLOYEE_ID")
    @GeneratedValue
    private Long employeeId;

    @Column(name="FIRSTNAME")
    private String firstname;

    @ManyToMany(cascade = {CascadeType.ALL})
    @JoinTable(name="EMPLOYEE_MEETING", 
                joinColumns={@JoinColumn(name="EMPLOYEE_ID")}, 
                inverseJoinColumns={@JoinColumn(name="MEETING_ID")})
    private Set<meeting> meetings = new HashSet<meeting>();

    public Employee() {
    }

    public Employee(String firstname) {
        this.firstname = firstname;            
    }
}

and meetings:

@Entity
@Table(name="MEETING")
public class Meeting {

    @Id
    @Column(name="MEETING_ID")
    @GeneratedValue
    private Long meetingId;

    @Column(name="SUBJECT")
    private String subject;

    @ManyToMany(mappedBy="meetings")
    private Set<employee> employees = new HashSet<employee>();

    public Meeting(String subject) {
        this.subject = subject;            
    }
}

Main snippet:

        Meeting meeting1 = new Meeting("Constant Subject");
        Meeting meeting2 = new Meeting("Constant Subject");

        Employee employee1 = new Employee("Sergey");
        Employee employee2 = new Employee("Larry");

        employee1.getMeetings().add(meeting1);            
        employee2.getMeetings().add(meeting2);

        session.save(employee1);
        session.save(employee2);

Please, consider that I deliberately created two Meeting objects with constant name. In this case, MEETING TABLE will persist TWO different meeting rows, for instance:

     MEETING                                      EMPLOYEE_MEETING
    Meeting_Id Subject                            Employee_Id   Meeting_Id
    1          Constant Subject                   1             1
    2          Constant Subject                   2             2 

Since I will have only a few constant values as Meeting Subjects I would like to prevent duplicates, and after executing the same code to have this situation:

    MEETING                                      EMPLOYEE_MEETING
    Meeting_Id Subject                            Employee_Id   Meeting_Id
    1         Constant Subject                    1             1
                                                  2             1

I want Hibernate to realize that Meeting table is set of constants and not to duplicate rows if not neccesarry. I've tried with unique constraint but it did not help. Is there a way to achieve this?

Upvotes: 0

Views: 111

Answers (1)

Akshay Mahajan
Akshay Mahajan

Reputation: 81

You are using generated ID for meeting table. and since you are using new objects without overriding equals and hashcode, hibernate has no way to realize that is single object. It will insert multiple objects since those are two different meetings from a DB standpoint (identified by separate ID for each).

Perhaps if the meeting subject is the unique constraint, change your definitions not to have generated ID field, rather keep meeting subject column as unique. Consequently, remove meetingId from your bean and just keep subject. Then you can use unique=true inside the @Column annotation for subject.

Upvotes: 2

Related Questions