-->
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.  [ 6 posts ] 
Author Message
 Post subject: failed to lazily initialize a collection
PostPosted: Thu Jan 19, 2006 3:17 am 
Newbie

Joined: Wed Jan 18, 2006 4:50 pm
Posts: 7
Dear All:
I have another hibernate problem that I really need your help, I am now using Hibernate 3.1, and I am trying to initialize a collection after I load a object. But I got an error saying no session or session is closed. The HibernateUtil class and the collection initialization part of the code I wrote are as of following. Please help me out.

Hibernate version: 3.1


***
Code of Hibernateutile class:
public class HibernateUtil {
......
public static final ThreadLocal session = new ThreadLocal();
public static Session currentSession()
throws HibernateException {
closeSession();

Session s = (Session) session.get();

// Open a new Session, if this Thread has none yet
if ((s == null) || !s.isOpen()) {
s = sessionFactory.openSession();
session.set(s);
}

return s;
}


/**
*
*
* @throws HibernateException
*/
public static void closeSession()
throws HibernateException {
Session s = (Session) session.get();
session.set(null);

if (s != null)
s.close();
}
.....
}
***


***
The function where I initialize the collection:
private void initializeMy Collection(myClass)throws HibernateException{
Session hSession = HibernateUtil.currentSession();
myClass.getMyCollection();
//I also tried Hibernate.initialize(myClass.getMyCollection()); but it does not work either
HibernateUtil.closeSession();
}
***



Full stack trace of any exception that occurs:
Jan 18, 2006 11:05:09 PM org.hibernate.LazyInitializationException <init>
SEVERE: failed to lazily initialize a collection of role: net.geocortex.charlotte.cdot.cras.persistence.Gisvalue.locationResearchs, no session or session was closed
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: net.geocortex.charlotte.cdot.cras.persistence.Gisvalue.locationResearchs, no session or session was closed
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:358)
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:350)
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:343)
at org.hibernate.collection.AbstractPersistentCollection.write(AbstractPersistentCollection.java:183)
at org.hibernate.collection.PersistentSet.add(PersistentSet.java:165)


Name and version of the database you are using:Microsoft SQLServer 2000


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 19, 2006 4:03 am 
Newbie

Joined: Wed Jan 11, 2006 5:21 am
Posts: 14
Try using the following:
Code:
private void initializeMy Collection(myClass)
throws HibernateException
{
   Session hSession = HibernateUtil.currentSession();
   hSession.refresh(myClass);
   myClass.getCollection();
   HibernateUtil.closeSession();
}



Top
 Profile  
 
 Post subject: a new error
PostPosted: Thu Jan 19, 2006 1:30 pm 
Newbie

Joined: Wed Jan 18, 2006 4:50 pm
Posts: 7
Hi Rdonasco:
Thanks very much for your reply.
I tried what you said in your post, and it won't give me the session is closed error anymore. However, this time I got a different error:

Jan 19, 2006 9:21:04 AM org.hibernate.util.JDBCExceptionReporter logExceptions
SEVERE: [Microsoft][SQLServer 2000 Driver for JDBC]ResultSet can not re-read row data for column 1.
ERROR OCCURRED
org.hibernate.exception.GenericJDBCException: could not initialize a collection: [net.geocortex.charlotte.cdot.cras.persistence.Myclass.myCollection#34]
at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:91)
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:79)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.loader.Loader.loadCollection(Loader.java:1926)
at org.hibernate.loader.collection.CollectionLoader.initialize(CollectionLoader.java:71)
at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:520)
at org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:60)
at org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1627)
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:344)
at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:86)
at org.hibernate.collection.PersistentSet.toString(PersistentSet.java:253)
at java.lang.String.valueOf(String.java:2131)
at java.lang.StringBuffer.append(StringBuffer.java:370)


Top
 Profile  
 
 Post subject: I found the problem.
PostPosted: Thu Jan 19, 2006 2:55 pm 
Newbie

Joined: Wed Jan 18, 2006 4:50 pm
Posts: 7
Hi Rdonasco:
I think I found the cause of the problem I shown you above.
There is a field of type float in the class which I should set to be of the type Float, so that this field will be an Object. The reason for this is there are some rows in the database table that holds null values for this field, that's the reason hibernate can not read these value out.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 19, 2006 11:56 pm 
Regular
Regular

Joined: Tue Dec 14, 2004 5:21 am
Posts: 104
Location: india
is myClass a detached object ? in that case , session.lock(myClass,LockMode.NONE) will help u . try loading collection after u reattach myClass.

_________________
sHeRiN
thanks for your ratings ...... :)


Top
 Profile  
 
 Post subject: Re: I found the problem.
PostPosted: Thu Jan 26, 2006 4:44 am 
Newbie

Joined: Wed Jan 11, 2006 5:21 am
Posts: 14
yzyu wrote:
Hi Rdonasco:
I think I found the cause of the problem I shown you above.
There is a field of type float in the class which I should set to be of the type Float, so that this field will be an Object. The reason for this is there are some rows in the database table that holds null values for this field, that's the reason hibernate can not read these value out.


That's good then, can you give me credits for this? I'm running out of credits already..

Kind regards,
Roy


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