-->
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.  [ 8 posts ] 
Author Message
 Post subject: Retrieve collection child from a parent
PostPosted: Thu Dec 11, 2003 6:08 am 
Beginner
Beginner

Joined: Thu Oct 02, 2003 8:02 am
Posts: 45
I have 2 classes User and Event with a one-to-many monodirectionnal association.
Code:
public class User
{
     private Set events;
     //...
}

Given a user (eg u), i would like to retrieve all his events.
I use MySQL.
I write the following request inside find method :
Code:
session.find("select elements(user.events) from User user where user=?",u,Hibernate.entity(User.class))


But MySQL version does not support sub-queries.
what kind of hibernate query can i write to do so ?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 11, 2003 8:54 am 
Beginner
Beginner

Joined: Thu Oct 02, 2003 8:02 am
Posts: 45
This is the good version of my question.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 11, 2003 10:46 am 
Expert
Expert

Joined: Tue Sep 16, 2003 4:06 pm
Posts: 318
Location: St. Petersburg, Russia
Why can't you just call user.getEvents() and let Hibernate do the job?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 11, 2003 11:00 am 
Beginner
Beginner

Joined: Thu Oct 02, 2003 8:02 am
Posts: 45
OK but does user.getEvents() must be called in a session ?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 11, 2003 11:04 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
If it is lazyly initialized, yes. If you have loaded the User Object in another Session, you can reassociate it with the new session by using session.lock(user, LockMode.NONE)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 11, 2003 11:06 am 
Expert
Expert

Joined: Tue Sep 16, 2003 4:06 pm
Posts: 318
Location: St. Petersburg, Russia
What does it mean "in a session" ?
In any case, user.getEvents() does not require Session as argument. Depending on your mapping (namely, "lazy" attribute of this <set>) you may need that session in which User object was loaded remain open when you call user.getEvents.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 11, 2003 11:11 am 
Beginner
Beginner

Joined: Thu Oct 02, 2003 8:02 am
Posts: 45
In fact, the user is store in a HttpSession Object and hibernate session in which user has been loaded is close. So if i retrieve user from HttpSession and create a new hibernate session, will user.getEvents() works ?
For information, i set lazy attribute to 'true"


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 11, 2003 11:12 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
As I said, use session.lock(user, LockMode.NONE) with the new session


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 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.