-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Question about one-to-many relation
PostPosted: Fri Feb 02, 2007 7:46 am 
Newbie

Joined: Wed Jan 24, 2007 10:29 am
Posts: 5
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


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 05, 2007 12:13 am 
Expert
Expert

Joined: Tue Jan 30, 2007 12:45 am
Posts: 283
Location: India
Hi zhlmmc,

try this.

User instance = (User) getSession()
.get(User.class, id);

_________________
Dharmendra Pandey


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.