-->
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.  [ 5 posts ] 
Author Message
 Post subject: Yo, my custom UserType getting LazyInitializationException
PostPosted: Mon Feb 16, 2004 7:00 pm 
Beginner
Beginner

Joined: Wed Oct 08, 2003 4:22 pm
Posts: 29
Hello,

I wrote a UserType to serialize a Map variable to a varchar column. I included the code for this below. It works fine for inserting. But when I attempt to retrieve (after restarting JBoss) the parent business object, TaskVO, I get a LazyInitializationException.

Any ideas on the cause? FYI I'm retrieving a collection of TaskVOs via a stateless Session Bean, so EJBs are involved. One other note - our serializer uses an XML Serializer we wrote because Sun's XmlEncoder is so poor compared to C# XmlSerializer.

Here is the descriptor for TaskVO business object:

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
   <class name="com.teradata.tap.system.scheduler.TaskVO" table="TAP_TASK">
      <id name="id" column="ID" type="long">
         <generator class="net.sf.hibernate.tap.SessionBeanSequenceGenerator"/>
      </id>
      <property name="label" column="Label"/>
      <property name="className" column="ClassName"/>
      <property name="parameters" column="Parameters" type="net.sf.hibernate.tap.MapUserType"/>
   </class>
</hibernate-mapping>


Here is the stack trace:

[code]
11:23:41,533 ERROR [LazyInitializationException] Failed to lazily initialize a c
ollection - no Session
net.sf.hibernate.LazyInitializationException: Failed to lazily initialize a coll
ection - no Session
at net.sf.hibernate.collection.PersistentCollection.initialize(Persisten
tCollection.java:167)
at net.sf.hibernate.collection.PersistentCollection.read(PersistentColle
ction.java:64)
at net.sf.hibernate.collection.Set.equals(Set.java:384)
at java.util.ArrayList.indexOf(ArrayList.java:216)
at java.util.ArrayList.contains(ArrayList.java:197)
at com.teradata.tap.system.xml.Decoder.startNode(Decoder.java:187)
at com.teradata.tap.system.xml.Handler.startElement(Handler.java:45)
at org.apache.xerces.parsers.AbstractSAXParser.startElement(AbstractSAXP
arser.java:452)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanStartElemen
t(XMLDocumentFragmentScannerImpl.java:821)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContent
Dispatcher.dispatch(XMLDocumentFragmentScannerImpl.java:1541)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(XM
LDocumentFragmentScannerImpl.java:346)
at org.apache.xerces.parsers.DTDConfiguration.parse(DTDConfiguration.jav
a:529)
at org.apache.xerces.parsers.DTDConfiguration.parse(DTDConfiguration.jav
a:585)
at org.apache.xerces.parsers.XMLParser.parse(XMLParser.java:152)
at org.apache.xerces.parsers.AbstractSAXParser.parse(AbstractSAXParser.j
ava:1142)
at javax.xml.parsers.SAXParser.parse(SAXParser.java:345)
at com.teradata.tap.system.xml.Decoder.<init>(Decoder.java:64)
at net.sf.hibernate.tap.MapUserType.nullSafeGet(MapUserType.java:112)
at net.sf.hibernate.type.CustomType.nullSafeGet(CustomType.java:97)
at net.sf.hibernate.type.AbstractType.hydrate(AbstractType.java:66)
at net.sf.hibernate.loader.Loader.hydrate(Loader.java:419)
at net.sf.hibernate.loader.Loader.loadFromResultSet(Loader.java:373)
at net.sf.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:342)
at net.sf.hibernate.loader.Loader.getRow(Loader.java:281)
at net.sf.hibernate.loader.Loader.doFind(Loader.java:159)
at net.sf.hibernate.loader.Loader.loadCollection(Loader.java:602)
at net.sf.hibernate.loader.OneToManyLoader.initialize(OneToManyLoader.ja
va:102)
at net.sf.hibernate.impl.SessionImpl.initialize(SessionImpl.java:2897)
at net.sf.hibernate.collection.PersistentCollection.getInitialValue(Pers
istentCollection.java:128)
at net.sf.hibernate.type.PersistentCollectionType.getCollection(Persiste
ntCollectionType.java:74)
at net.sf.hibernate.type.PersistentCollectionType.resolveIdentifier(Pers
istentCollectionType.java:177)
at net.sf.hibernate.impl.SessionImpl.initializeEntity(SessionImpl.java:1
959)
at net.sf.hibernate.loader.Loader.doFind(Loader.java:196)
at net.sf.hibernate.loader.Loader.find(Loader.java:620)
at net.sf.hibernate.loader.CriteriaLoader.list(CriteriaLoader.java:81)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:3157)
at net.sf.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:65)
at com.teradata.tap.system.persistence.ejb.PersistenceManagerBean.getVec
tor(PersistenceManagerBean.java:1334)


And here is my MapUserType, nullSafeGet function:

[code]

public Object nullSafeGet(ResultSet rs, String[] names, Object owner) throws HibernateException, SQLException {

// Only 1 column
Object obj = rs.getObject(names[0]);
if (obj != null && obj.toString().length() > 0) {

Map map = null;
String input = obj.toString();

try{
Decoder decoder = new Decoder((String)input);
map = (Map) decoder.getObject();
}
catch(Exception e){
e.printStackTrace();
}

return map;
}

return null;
}
[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 16, 2004 7:14 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Your Decoder is calling contains on a mapped collection at com.teradata.tap.system.xml.Decoder.startNode, and I strongly suspect you can't do that while the object is still constructed by Hibernate.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 16, 2004 7:43 pm 
Beginner
Beginner

Joined: Wed Oct 08, 2003 4:22 pm
Posts: 29
Well we are trying to understand exactly how Hibernate's Set constructor works. When we execute the following code, we get the same LazyInitializationException thrown. In our code, the Hibernate exception is always thrown on the call to .equals.

Why?

Code:

try{
      net.sf.hibernate.collection.Set hs = new net.sf.hibernate.collection.Set();
      if (hs.equals("Will it Fail")){
         System.out.println("Hibernate Set Equals method returned true..");
      }else{
          System.out.println("Hibernate Set Equals method returned false..");
      }
}catch(Exception e){
      e.printStackTrace();
      System.out.println("Error in hibernate set equals method..");
}


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 17, 2004 3:45 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
An Hibernate collection must be binded to a session. If not or if the session is closed, accessing the collection (if not previously initialized) will raise a LazyInitializationException.
equals() have to load the collection before comparing it (and it drives Gavin to despair ;-) )

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 19, 2004 8:42 pm 
Beginner
Beginner

Joined: Wed Oct 08, 2003 4:22 pm
Posts: 29
Fyi, I ended up changing the client code so that it didn't attempt to serialize a Set using our UserType. But long term would be nice to find a away to accomplish this.


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