Biscuit128
Biscuit128

Reputation: 5398

How to use join tables with hibernate

I am having some difficulty using hibernate to select using join tables that represent a many to many.

I have the following three tables (bold represents the links / keys)

User {id, username, password}

Group {id, name, scpid}

Join table = Member {id, groupId, username}

So I have the scenario that in my DAO for user and group i want to get the available groups and the available members respectively.

This means I need to provide the mapping but am unsure how to do this. So far to get the groups for a user i have tried something like this but it complains about a duplicate username

@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinTable(name = "member", joinColumns = {@JoinColumn(name = "username", nullable = false, updatable = false)}, inverseJoinColumns = {@JoinColumn(name = "username", nullable = false, updatable = false)})
public Set<Group> getGroups()
{
    return userGroups;
}

public void setGroups(Set<Group> userGroups)
{
    this.userGroups = userGroups;
}

Can anyone help me in identifying how I would solve this please?

Thanks

Upvotes: 1

Views: 670

Answers (1)

AimZ
AimZ

Reputation: 536

According to provided code snippet both @JoinColumn's name attribute is having username as value.

Upvotes: 3

Related Questions