I have some problem with the one-to-many relation.
I have a Class User and a Class UserLevel. The relation between User and UserLevel is 1-n.
So, I have following configurations:
In User.hbm.xml
Code:
<set name="userLevels" inverse="true">
<key>
<column name="user_id" />
</key>
<one-to-many class="yuguo.its.data.model.UserLevel" />
</set>
In User.java
Code:
private Set userLevels = new HashSet(0);
In UserDAO.java
Code:
public User findById( java.lang.Integer id) {
log.debug("getting User instance with id: " + id);
try {
User instance = (User) getSession()
.get("yuguo.its.data.model.User", id);
return instance;
} catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
}
I use User.findById to get an instance of User. Then I use user.getUserLevels() to get the UserLevel set. In the database, I have two UserLevel assigned to the User. But the set returned by user.getUserLevels() is empty.
When I tried UserLevelDAO.getByExample(new UserLevel(User)), the returned list is correct.
My Hibernate version is 3.1.
What's the problem?
Thanks in advance!
Hailong Zhang