-->
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: session closed exception
PostPosted: Thu Dec 02, 2004 6:52 am 
Beginner
Beginner

Joined: Thu Dec 02, 2004 4:49 am
Posts: 32
Can someone tell me why i am getting the following exception?
i am getting the session...doing some transactions....and then closing the session..then why is session getting closed in the middle?
has it got anything to do with find method?

I am very new to this Hibernate...and searched in the forums also...but could not really get what i wanted...

Thanks,
SSSS.


Hibernate version:2

<class name="com.sat.helper.PrivilegeDB" table="PRIVILEGE">
<id name="id" type="string" column="PRIVILEGE_ID">
<generator class="uuid.hex"/>
</id>
<property name="name" column="PRIVILEGE_NAME"/>
<set name="roles" table="ROLE_PRIV_MAP" inverse="true" lazy="true" >
<key column="PRIVILEGE_ID"/>
<one-to-many class="com.sat.helper.PrivRoleMapDB"/>
</set>
</class>
<class name="com.sat.helper.PrivRoleMapDB" table="ROLE_PRIV_MAP">
<composite-id>
<key-many-to-one name="privilege" class="com.sat.helper.PrivilegeDB" column="PRIVILEGE_ID" />
<key-many-to-one name="role" class="com.sat.helper.RoleDB" column="ROLE_ID" />
</composite-id>
</class>

<class name="com.sat.helper.RoleDB" table="ROLE">
<id name="id" type="string" column="ROLE_ID">
<generator class="uuid.hex"/>
</id>
<property name="lastUpdateDate" type="calendar" column="LAST_UPDATE_INFO"/>
<property name="name" column="ROLE_NAME"/>
<set name="privRoleMaps" table="ROLE_PRIV_MAP" inverse="true" lazy="true" >
<key column="ROLE_ID"/>
<one-to-many class="com.sat.helper.PrivRoleMapDB"/>
</set>
</class>


Session session = getSession();
Transaction trans = session.beginTransaction();
List rolesList = getHibernateTemplate().find("from RoleDB");
if(rolesList != null){
int listSize = rolesList.size();
for(int i=0;i<listSize;i++){
RoleDB roleDB = (RoleDB) rolesList.get(i);
System.out.println("role id and name are " + roleDB.getId() + "and " + roleDB.getName());
Set privs = roleDB.getPrivRoleMaps();
if(privs != null) {
Iterator iter = privs.iterator();
while(iter.hasNext()){
PrivRoleMapDB p = (PrivRoleMapDB) iter.next();
System.out.println("priv id and name are " + p.getPrivilege().getId() + "and " + p.getPrivilege().getName());
}
} else{
System.out.println("privs are null for this role");
}
}
}
session.flush();
trans.commit();
session.close();


SEVERE: Failed to lazily initialize a collection - no session or session was cl
sed
net.sf.hibernate.LazyInitializationException: Failed to lazily initialize a col
ection - no session or session was closed
at net.sf.hibernate.collection.PersistentCollection.initialize(Persiste
tCollection.java:209)
at net.sf.hibernate.collection.PersistentCollection.read(PersistentColl
ction.java:71)
at net.sf.hibernate.collection.Set.iterator(Set.java:130)
at com.sat.dao.DAO.getRoles(DAO.java:263)
at com.sat.FacadeImpl.getRoles(FacadeImpl.java:136)
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.springframework.aop.framework.AopProxyUtils.invokeJoinpointUsing
eflection(AopProxyUtils.java:59)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynam
cAopProxy.java:138)
at $Proxy0.getRoles(Unknown Source)
at com.sat.FacadeImpl.main(FacadeImpl.java:152)
net.sf.hibernate.LazyInitializationException: Failed to lazily initialize a col
ection - no session or session was closed
at net.sf.hibernate.collection.PersistentCollection.initialize(Persiste
tCollection.java:209)
at net.sf.hibernate.collection.PersistentCollection.read(PersistentColl
ction.java:71)
at net.sf.hibernate.collection.Set.iterator(Set.java:130)
at com.sat.dao.DAO.getRoles(DAO.java:263)
at com.sat.FacadeImpl.getRoles(FacadeImpl.java:136)
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.springframework.aop.framework.AopProxyUtils.invokeJoinpointUsing
eflection(AopProxyUtils.java:59)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynam
cAopProxy.java:138)
at $Proxy0.getRoles(Unknown Source)
at com.sat.FacadeImpl.main(FacadeImpl.java:152)
java.lang.NullPointerException
at com.sat.FacadeImpl.main(FacadeImpl.java:153)


Name and version of the database you are using: DB2 7.1

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 02, 2004 6:56 am 
Expert
Expert

Joined: Tue Oct 05, 2004 9:45 am
Posts: 263
i don't know exactly, because you haven't posted the hole relevant code ... but it seems for me, that your session-handling is broken ...

gtx
curio


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 02, 2004 7:01 am 
Beginner
Beginner

Joined: Thu Dec 02, 2004 4:49 am
Posts: 32
but that is all the code that i wrote..

DAO extends HibernateDaoSupport
public Role[] getRoles() {

Session session = getSession();
Transaction trans = session.beginTransaction();
List rolesList = getHibernateTemplate().find("from RoleDB");
if(rolesList != null){
int listSize = rolesList.size();
for(int i=0;i<listSize;i++){
RoleDB roleDB = (RoleDB) rolesList.get(i);
System.out.println("role id and name are " + roleDB.getId() + "and " + roleDB.getName());
Set privs = roleDB.getPrivRoleMaps();
if(privs != null) {
Iterator iter = privs.iterator();
while(iter.hasNext()){
PrivRoleMapDB p = (PrivRoleMapDB) iter.next();
System.out.println("priv id and name are " + p.getPrivilege().getId() + "and " + p.getPrivilege().getName());
}
} else{
System.out.println("privs are null for this role");
}
}
}
session.flush();
trans.commit();
session.close();


}


i am calling this method from a class which has main method...that is all what i am doing...am i missing something????

Thanks,
SSSS


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 02, 2004 7:48 am 
Expert
Expert

Joined: Tue Oct 05, 2004 9:45 am
Posts: 263
well if that's all the code you've written ...

where comes the
Code:
getHibernateTemplate().find("from RoleDB");
and
Code:
getSession()
methods from? ;-)

Check your sources if you're using one session for one Unit of Work ...

gtx
curio


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 02, 2004 7:54 am 
Beginner
Beginner

Joined: Thu Dec 02, 2004 4:49 am
Posts: 32
My DAO is extending HibernateDaoSupport...so i can use
getHibernateTemplate() and getSession() methods...

i didn't really understand what was wrong with that...

what is meant a single unit of work?

here i am asking for a single select query...

where am i breaking the rules??

Thanks,
SSSS.


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.