What is the difference between Unidirectional and Bidirectional associations?
Posted
by
hguser
on Stack Overflow
See other posts from Stack Overflow
or by hguser
Published on 2011-03-19T07:28:02Z
Indexed on
2011/03/19
8:10 UTC
Read the original article
Hit count: 257
hibernate
|associations
Hi:
What is the difference between Unidirectional and Bidirectional associations?
Since the table generated in the db are all the same,so the only difference I found is that each side of the bidiretional assocations will have a refer to the other,and the unidirectional not.
/////////// This is a Unidirectional association
public class User {
private int id;
private String name;
@ManyToOne
@JoinColumn(
name = "groupId")
private Group group;
}
public class Group {
private int id;
private String name;
}
////////////// The Bidirectional association
public class User {
private int id;
private String name;
@ManyToOne
@JoinColumn(
name = "groupId")
private Group group;
}
public class Group {
private int id;
private String name;
@OneToMany(mappedBy="group")
private List<User> users;
}
The difference is wheather the group hold a refer of the user.
So I wonder if this is the only difference? which is recommended?
© Stack Overflow or respective owner