Rahul Agrawal
Rahul Agrawal

Reputation: 8971

Hibernate Many to Many Mapping with Additional Column?

I need to have additional column in manytomany generated table as

there are 2 entities which are related with each other as many to many (User ManytoMany Group)

@Entity
public class User {
    //other fields
    private Set<Group> groups = new HashSet<Group>(0);
}

@Entity
public class Group {
    //other fields
    private Set<User> users = new HashSet<User>(0);
}

So here it generates 3 tables in database as

table1 - User

table2 - Group

table3 - UserGroup

Now I need additional field in UserGroup table, so how to specify that and in which entity?

Additonal field will be

private boolean isThisUserIsGroupManager;

So that, i can get that user of this group is a group manager also.

Upvotes: 2

Views: 10823

Answers (3)

Mikko Maunu
Mikko Maunu

Reputation: 42084

Relationships do not have persistent attributes to set, only entities do have. That's why you need third entity between. It will have two @Id attributes and consequently @IdClass is needed, because identity is derived from Group and User:

@Entity
public class User {
    @Id int id;    
    @OneToMany(mappedBy = "user")
    private Set<GroupMemberShip> groupMemberShips;
    ...
}

@Entity
public class Group {
    @Id int id;
    @OneToMany(mappedBy = "group")
    private Set<GroupMemberShip> groupMemberShips;
    ...
}


@Entity
@IdClass(GroupMemberShipId.class)
public class GroupMemberShip {
    @Id @ManyToOne User user;

    @Id @ManyToOne Group group;

    private boolean isThisUserIsGroupManager;
    ...
}

public class GroupMemberShipId implements Serializable {
    int user;
    int group;
    ...
}

Upvotes: 6

AbVog
AbVog

Reputation: 1555

Actually, you can do many to many associations, including with extra columns.

First, many to many mapping is a Hibernate feature. You'll find it in the Hibernate (3.5.1-final) Reference in the "Mapping associations" section of chapter 1. Just search for "many-to-many" (in the PDF file that I have, section "1.2.2. A unidirectional Set-based association" is what you're looking for - it has an example on pages 14 and 14 just like yours). So maybe I am confused as to what the other responders are referring to when they say that you need to use two many to one mappings, since the aforementioned example also has three tables.

Second, for an example of using additional columns, see this tutorial on Mkyong.com

The thing to keep in mind, and that the tutorial doesn't make explicit, is that the table that stores the association info normally has NO extra columns, which implicitly makes the primary key a composite of the participating entities's keys: the key in UserGroup combines user.id and group.id. So you need the @AssociationOverride tag to specify which of the fields make up the key since if you didn't, Hibernate would use all fields as

Whether it is good practice or not is a different issue, as is whether or not it will improve performance when compared with having a separate entity (e.g. UserGroupInfo, which would be a fourth one in your case) of which the sole purpose is to hold the extra columns (UserGroupInfo will then have to reuse UserGroup's id).

Upvotes: 1

Subir Kumar Sao
Subir Kumar Sao

Reputation: 8401

You cannot do that directly in Hibernate.

You need to do Many - One and then One - Many mapping.

You need an extra column on the mapping table. I assume you will also be setting some value to it. You cannot do that without having a entity mapped to that table.

Upvotes: 1

Related Questions