Im new at Hibernate and i have a problem that i can't solve, i take a look at similar questions but i haven't luck...
I have a table (lets call it T1) and another table (lets call it T2). T1 have a relation with T2 and T2 same with T1, so they have a many to many relation so i created a bridge table (lets call it T1_T2).
T1.id <----> T1_T2.T1id <----> T2.id T2.id <----> T1_T2.T2id <----> T1.id
My question is the next: How i should build my java clases (T1, T1_T2, T2) and make a query with criteria, only giving one id (of any of the 2 tables) as a parameter?
Hope anyone can help me, thanks!
You could use the annotation @JoinTable after @ManyToMany pointing the t1_t2 table for relation. See http://en.wikibooks.org/wiki/Java_Persistence/ManyToMany
Let me put some example, One User has many permissions and one Permission has many Users
User
@Table(name="USER")
public class User() {
@Id
private Integer id;
@ManyToMany
@JoinTable(
name="USER_GROUP",
joinColumns={@JoinColumn(name="USER_ID")},
inverseJoinColumns={@JoinColumn(name="GROUP_ID")})
private List<Group> groups;
...
}
Permission
@Table(name="GROUP")
public class Group(){
@Id
private Integer id;
@JoinTable(
name="USER_GROUP",
joinColumns={@JoinColumn(name="GROUP_ID")},
inverseJoinColumns={@JoinColumn(name="USER_ID")})
private List<Users> users;
...
}