-->
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: LazyException while loading a collection through named query
PostPosted: Tue Dec 07, 2010 6:33 pm 
Newbie

Joined: Tue Dec 07, 2010 6:11 pm
Posts: 1
Hi,
I am using Hibernate 3.2 in my project. My database is Oracle 10g. I am trying to initialize a set using a named query. I am getting a LazyInitialization exception.

Please let me know what am I missing. Any help would be highly appreciated.

Thanks
Anuradha

<b>Log messages including stack trace</b>

INFO : org.hibernate.cache.StandardQueryCache - starting query cache at region: org.hibernate.cache.StandardQueryCache
DEBUG: org.hibernate.SQL -
/* criteria query */ select
this_.ID as ID2_2_,
this_.NAME as NAME2_2_,
this_.permission_Object_Id as permission3_2_2_,
this_.category_id as category4_2_2_,
this_.restriction as restrict5_2_2_,
this_.permission_Object_Id as formula0_2_,
this_.datatype_id as formula1_2_,
permission2_.ID as ID1_0_,
permission2_.NAME as NAME1_0_,
datatype3_.ID as ID8_1_,
datatype3_.DATA_TYPE as DATA2_8_1_
from
PERMISSION_OBJECT_FIELD_MAP this_
left outer join
PERMISSION_OBJECT permission2_
on this_.permission_Object_Id=permission2_.ID
left outer join
DATA_TYPE datatype3_
on this_.datatype_id=datatype3_.ID
where
this_.ID=?
TRACE: org.hibernate.type.IntegerType - binding '86' to parameter: 1
TRACE: org.hibernate.type.IntegerType - returning '6' as column: ID1_0_
TRACE: org.hibernate.type.IntegerType - returning '2' as column: ID8_1_
TRACE: org.hibernate.type.IntegerType - returning '86' as column: ID2_2_
TRACE: org.hibernate.type.StringType - returning 'METADATA' as column: NAME1_0_
TRACE: org.hibernate.type.StringType - returning 'Double' as column: DATA2_8_1_
TRACE: org.hibernate.type.StringType - returning 'denomination' as column: NAME2_2_
TRACE: org.hibernate.type.IntegerType - returning '6' as column: permission3_2_2_
TRACE: org.hibernate.type.IntegerType - returning null as column: category4_2_2_
TRACE: org.hibernate.type.YesNoType - returning 'true' as column: restrict5_2_2_
TRACE: org.hibernate.type.IntegerType - returning '6' as column: formula0_2_
TRACE: org.hibernate.type.IntegerType - returning '2' as column: formula1_2_
DEBUG: org.hibernate.SQL -
/* named native SQL query simpleoperators */ select
o.*
from
Operator o,
Type_Operator_Map tom,
Permission_Object_Field_Map p
where
o.id = tom.operator_Id
and tom.dataType_Id = p.dataType_Id
and p.id = ?
TRACE: org.hibernate.type.IntegerType - binding '86' to parameter: 1
TRACE: org.hibernate.type.BigDecimalType - returning '1' as column: ID
TRACE: org.hibernate.type.StringType - returning '=' as column: OPERATOR
TRACE: org.hibernate.type.BigDecimalType - returning '2' as column: ID
TRACE: org.hibernate.type.StringType - returning '<' as column: OPERATOR
TRACE: org.hibernate.type.BigDecimalType - returning '3' as column: ID
TRACE: org.hibernate.type.StringType - returning '>' as column: OPERATOR
DEBUG: org.hibernate.SQL -
/* named native SQL query simpleoperators */ select
o.*
from
Operator o,
Type_Operator_Map tom,
Permission_Object_Field_Map p
where
o.id = tom.operator_Id
and tom.dataType_Id = p.dataType_Id
and p.id = ?
TRACE: org.hibernate.type.IntegerType - binding '86' to parameter: 1
TRACE: org.hibernate.type.BigDecimalType - returning '1' as column: ID
TRACE: org.hibernate.type.StringType - returning '=' as column: OPERATOR
TRACE: org.hibernate.type.BigDecimalType - returning '2' as column: ID
TRACE: org.hibernate.type.StringType - returning '<' as column: OPERATOR
TRACE: org.hibernate.type.BigDecimalType - returning '3' as column: ID
TRACE: org.hibernate.type.StringType - returning '>' as column: OPERATOR
ERROR: org.hibernate.LazyInitializationException - failed to lazily initialize a collection of role: com.eq.del.docstore.model.permission.PermissionObjectFieldMap.operators, no session or session was closed
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.eq.del.docstore.model.permission.PermissionObjectFieldMap.operators, 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.read(AbstractPersistentCollection.java:86)
at org.hibernate.collection.PersistentSet.toString(PersistentSet.java:309)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at com.eq.del.docstore.test.permission.PermissionMetadataTest.getOperators(PermissionMetadataTest.java:38)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:240)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:46)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:180)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:41)
at org.junit.runners.ParentRunner$1.evaluate(ParentRunner.java:173)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:220)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:38)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)

Here's my configuration:

PermissionObjectFieldMap.hbm.xml

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping default-access="field" package="com.eq.del.docstore.model.permission">

    <!-- Hibernate mapping -->
    <class name="PermissionObjectFieldMap" table="PERMISSION_OBJECT_FIELD_MAP" lazy="false">
       <cache usage="read-write"  region="domainCache"/>
       <id name="id" type="java.lang.Integer" column="ID">
           <generator class="sequence-identity">
              <param name="sequence">PERMISSION_FIELD_SEQ</param>
           </generator>
        </id>
        <property name="name"  type="java.lang.String" column="NAME"/>
        <property name="permissionObjectId" type="java.lang.Integer" column="permission_Object_Id"/>
        <property name="categoryId" type="java.lang.Integer" column="category_id"/>
        <property name="restriction" type="yes_no" column="restriction"/>
       
        <one-to-one name="permissionObject" class="PermissionObject" constrained="false" lazy="false">
           <formula>permission_Object_Id</formula>
        </one-to-one>
       
        <one-to-one name="datatype" class="Datatype" constrained="false" lazy="false">
           <formula>datatype_id</formula>
        </one-to-one>       

      <!-- <set name = "operators" table="Type_Operator_map" lazy="false" >
           <key column="datatype_id" />
           <one-to-many class="TypeOperatorMap"  />
        </set> -->
       
       <set name = "operators" table="Type_Operator_Map" lazy="false" cascade = "all">
             <key/>
           <many-to-many unique="true" class="Operator" lazy="false"/>           
           <loader query-ref ="simpleoperators"/>       
        </set>
       
       
    </class>

   
    <sql-query name = "simpleoperators">       
             <![CDATA[select o.* from Operator o, Type_Operator_Map tom, Permission_Object_Field_Map p where
            o.id = tom.operator_Id
            and tom.dataType_Id = p.dataType_Id
            and p.id = ?         
            ]]>
    </sql-query>
       
</hibernate-mapping>


DAO Class:

Code:
   @Override
   public Set<Operator> getOperators(Integer permissionFieldId) {
      PermissionObjectFieldMap map =  (PermissionObjectFieldMap)getSession().createCriteria(PermissionObjectFieldMap.class).add
      (Restrictions.eq("id", permissionFieldId))
      .uniqueResult();      
      //map.getOperators().size();   
      //getSession().update(map.getOperators());
      //Hibernate.initialize(map.getOperators());
      List<Operator> l = runNamedSQL();
      
      return map.getOperators();
   }


JUnit test
Code:
package com.eq.del.docstore.test.permission;

import static org.junit.Assert.assertEquals;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.eq.del.docstore.dao.permission.PermissionStore;
import com.eq.del.docstore.model.permission.PermissionFieldRegionMap;
import com.eq.del.docstore.model.permission.PermissionObjectFieldMap;
import com.eq.del.docstore.service.PermissionService;



@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "/docstore-config.xml")
public class PermissionMetadataTest {   
   @Autowired
   PermissionService permissionService;

   
   
   @Test
   public void getOperators() {
      System.out.println("Operators "+permissionService.getOperators(86)) ;
   }

}




CREATE TABLE PERMISSION_FIELD_REGION_MAP(
ID INTEGER PRIMARY KEY,
DISPLAY_NAME varchar2(200) NOT NULL,
REGION_ID integer NOT NULL,
PERMISSION_FIELD_ID integer NOT NULL
);

CREATE TABLE DATA_TYPE(
ID INTEGER PRIMARY KEY,
DATA_TYPE VARCHAR2(100) NOT NULL
);

CREATE TABLE OPERATOR(
ID INTEGER PRIMARY KEY,
OPERATOR VARCHAR2(100) NOT NULL
);

CREATE TABLE TYPE_OPERATOR_MAP(
DATATYPE_ID INTEGER NOT NULL,
OPERATOR_ID INTEGER NOT NULL
);


Top
 Profile  
 
 Post subject: Re: LazyException while loading a collection through named query
PostPosted: Wed Dec 08, 2010 4:45 am 
Beginner
Beginner

Joined: Fri Nov 26, 2010 8:25 am
Posts: 21
Hi there,

Could you post the whole of your DAO class please?

Many thanks,
Kate.


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.