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.  [ 7 posts ] 
Author Message
 Post subject: How to detach a collection?
PostPosted: Mon Mar 15, 2004 4:31 pm 
Newbie

Joined: Mon Mar 15, 2004 3:30 pm
Posts: 6
I have:

public Collection xxx(String username) {

Session session = HibernateUtil.currentSession();
Transaction tx = session.beginTransaction();
query = session.createQuery("...");

Collection resultList=query.list();


tx.commit();
HibernateUtil.closeSession();
return resultList;
}

But I get nothing returned, I guess it's because i close the session. I don't know when would be the best time to close a session and how to detach a object/collection. can someone help me ?


Top
 Profile  
 
 Post subject: try lazy=false
PostPosted: Mon Mar 15, 2004 4:44 pm 
Beginner
Beginner

Joined: Thu Sep 04, 2003 2:50 pm
Posts: 45
Location: US: New York NY
or you can iterate over the collection before you close the session. There may be other ways, not sure.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 15, 2004 7:33 pm 
Newbie

Joined: Mon Mar 15, 2004 3:30 pm
Posts: 6
sounds not a best way to do it. is there anyway like JDO does ses.attach/detach...?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 15, 2004 8:14 pm 
Beginner
Beginner

Joined: Thu Sep 04, 2003 2:50 pm
Posts: 45
Location: US: New York NY
try lazy=false. If you have lazy=true, the session will not populate your objects in the collection util you access them. If you don't use lazy=true, you have to iterate over them to get get hibernate to populate them.


Top
 Profile  
 
 Post subject: Re: How to detach a collection?
PostPosted: Tue Mar 16, 2004 4:51 am 
Expert
Expert

Joined: Fri Feb 06, 2004 7:49 am
Posts: 255
Location: Moscow, Russia
liudan wrote:
But I get nothing returned, I guess it's because i close the session. I don't know when would be the best time to close a session and how to detach a object/collection. can someone help me ?

liudan, everything is fine with session closing, the same approach works for me and collection returned by query.list() does not expire after session.close(). Check yor query: look into the hibernate debug level log, copy SQL query for your HQL one, repalce all ? with the corresponding values and execute this SQL query using your SQL tool, I guess it returns nothing.

Leonid


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 16, 2004 4:54 am 
Expert
Expert

Joined: Fri Feb 06, 2004 7:49 am
Posts: 255
Location: Moscow, Russia
liudan wrote:
sounds not a best way to do it. is there anyway like JDO does ses.attach/detach...?

I have not heard anything about attache/detach in Hibernate. About collections and objects returned by Hibenate query.list() I already thought like about detached ones.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 17, 2004 10:21 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
session.lock(object, LockMode.NONE)
or session.update() if the object may be dirty.

_________________
Emmanuel


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