0

I've done the necessary changes to my models outlined here. However, I don't know what to put on my join table entity.

Note that my join table has a surrogate key , and two extra columns (date and varchar).

What I've got so far is:

User.java

@Entity
@Table (name = "tbl_bo_gui_user")
@DynamicInsert
@DynamicUpdate
public class User implements Serializable {
    private String id;
    private String ntName;
    private String email; 
    private Set<GroupUser> groupUsers = new HashSet<GroupUser>(0);

    // Constructors and some getters setters omitted

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "pk.user", cascade=CascadeType.ALL)
    public Set<GroupUser> getGroupUsers() {
        return groupUsers;
    }

    public void setGroupUsers(Set<GroupUser> groupUsers) {
        this.groupUsers = groupUsers;
    }
}

Group.java

@Entity
@Table (name = "tbl_bo_gui_group")
@DynamicInsert
@DynamicUpdate
public class Group implements Serializable {
    private String id;
    private String groupName;
    private String groupDesc;
    private Set<GroupUser> groupUsers = new HashSet<GroupUser>(0);

    // Constructors and some getters setters omitted

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "pk.group", cascade=CascadeType.ALL)
    public Set<GroupUser> getGroupUsers() {
        return groupUsers;
    }

    public void setGroupUsers(Set<GroupUser> groupUsers) {
        this.groupUsers = groupUsers;
    }
}

The problem is that I don't know what to put on my join table entity. Here it is.

GroupUser.java

@Entity
@Table (name = "tbl_bo_gui_group_user")
@DynamicInsert
@DynamicUpdate
@AssociationOverrides({
    @AssociationOverride(name = "pk.user", 
            joinColumns = @JoinColumn(name = "id")),
    @AssociationOverride(name = "pk.group", 
            joinColumns = @JoinColumn(name = "id")) })
public class GroupUser implements Serializable {
    private String id;
    private User userId;
    private Group groupId;
    private Date dateCreated;
    private String createdBy;

    // constructors and getters and setters for each property

    // What now? ? No idea
}
makalshrek
  • 853
  • 3
  • 14
  • 29

1 Answers1

1

user to group would be a Many-To-Many relation. Now, you are splitting that up into Two One-To-Many Relations. Therefore your Mapping Entity simple needs to complete the Many-To-Many relation, by using Many-To-One:

public class GroupUser implements Serializable {
    private String id;

    @ManyToOne
    private User userId;

    @ManyToOne
    private Group groupId;
    private Date dateCreated;
    private String createdBy;
}

See also this example: Mapping many-to-many association table with extra column(s) (The Answer with 38 upvotes)

Community
  • 1
  • 1
dognose
  • 20,360
  • 9
  • 61
  • 107
  • How would I write the methods for saving the data of the users and groups in the model of the join table? There's `userService.setUser(user);` on the link that you gave but it didn't show a complete idea of an entity of a join table that I am looking for. I'm a hibernate newbie by the way. – makalshrek Oct 14 '13 at 09:52
  • @SciasTwentyThree If your entities are *set up properly* you dont need to care. I.e.: One User, 5 mappings, 5 Groups and each Group with 10 additional attributes: Call `userService.save(user)` and hibernate will persist all 61 entities ( 1 user + 5 mappings + 5 groups + 10 attribues per group (=50)) for you, because they are "connected". – dognose Oct 14 '13 at 19:38