-->
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: LazyInitializationExceptionon my ejb client
PostPosted: Thu Dec 07, 2006 4:22 am 
Newbie

Joined: Tue Oct 17, 2006 5:18 am
Posts: 9
Hi All

I'm new with hibernate, I had following problem working with hibernate

Hibernate version: 3.2

Mapping documents:

<class name="com.arkalogic.parkalogic.ejb.pojo.User" table="tbluser">
<id name="userid" column="user_id" type="integer"><generator class="native"/></id>
<property name="fullname" type="string"/>
<many-to-one name="typeshift" column="type_shift" class="com.arkalogic.parkalogic.ejb.pojo.UserShift" cascade="save-update" />
</class>

<class name="com.arkalogic.parkalogic.ejb.pojo.UserShift" table="user_shift">
<id name="id" column="gid" type="integer"><generator class="native"/></id>
<property name="shiftname" column="shift_name" type="string"/>
<property name="desc" column="description" type="string"/>
</class>


Code between sessionFactory.openSession() and session.close():

This is hibernate dao in my ejb

public User getUser(String username) {
User user = null;
try {
HibernateUtil.beginTransaction();
Session sess = HibernateUtil.getSession();
Query query = sess.createQuery("from User where username = :username").setString("username", username);
System.out.println("user query has next: " + query.iterate().hasNext());
user = (User) query.iterate().next();
if(user != null){
System.out.println("user fullname: " + user.getFullname());
}else{
System.out.println("user null");
}
} catch (Exception e) {
HibernateUtil.rollbackTransaction();
e.printStackTrace();
}
return user;
}


Full stack trace of any exception that occurs:

when I call getUser from ejb client, got the following expcetion

SEVERE: could not initialize proxy - no Session
org.hibernate.LazyInitializationException: could not initialize proxy - no Session
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:57)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:111)
at org.hibernate.proxy.pojo.cglib.CGLIBLazyInitializer.invoke(CGLIBLazyInitializer.java:172)
at com.arkalogic.parkalogic.ejb.pojo.UserShift$$EnhancerByCGLIB$$d2e6d969.getDesc(<generated>)
at helloclient.Main.testClient(Main.java:154)
at helloclient.Main.main(Main.java:68)
org.hibernate.LazyInitializationException: could not initialize proxy - no Session
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:57)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:111)
at org.hibernate.proxy.pojo.cglib.CGLIBLazyInitializer.invoke(CGLIBLazyInitializer.java:172)
at com.arkalogic.parkalogic.ejb.pojo.UserShift$$EnhancerByCGLIB$$d2e6d969.getDesc(<generated>)
at helloclient.Main.testClient(Main.java:154)
at helloclient.Main.main(Main.java:68)

While the code snippet ib my client is :

AdminFacadeRemote afs = (AdminFacadeRemote) lookupbeanAdmin().create(AppConfig.ADMINUSER);
AdminFacadeRemote afsa = (AdminFacadeRemote) lookupbeanAdmin().create(AppConfig.ADMINUSERSHIFT);
User u = (User) afs.getUser("admin");
System.out.println(u.getFullname());
System.out.println(u.getTypeshift().getDesc()); // this is line 154


Name and version of the database you are using:

I'm using mysql version 4.1.13

The generated SQL (show_sql=true):

The generated sql in my ejb server as a follow

[#|2006-12-07T15:16:47.335+0700|INFO|sun-appserver-pe9.0|javax.enterprise.system.stream.out|_ThreadID=15;_ThreadName=p: thread-pool-1; w: 7;|
Hibernate: select user0_.user_id as col_0_0_ from tbluser user0_ where user0_.username=?|#]

[#|2006-12-07T15:16:47.362+0700|INFO|sun-appserver-pe9.0|javax.enterprise.system.stream.out|_ThreadID=15;_ThreadName=p: thread-pool-1; w: 7;|
Hibernate: select user0_.user_id as col_0_0_ from tbluser user0_ where user0_.username=?|#]

[#|2006-12-07T15:16:47.381+0700|INFO|sun-appserver-pe9.0|javax.enterprise.system.stream.out|_ThreadID=15;_ThreadName=p: thread-pool-1; w: 7;|
Hibernate: select user0_.user_id as user1_0_0_, user0_.username as username0_0_, user0_.password as password0_0_, user0_.user_level as user4_0_0_, user0_.blocked_state as blocked5_0_0_, user0_.login_state as login6_0_0_, user0_.employee_id as employee7_0_0_, user0_.login_time as login8_0_0_, user0_.logout_time as logout9_0_0_, user0_.fullname as fullname0_0_, user0_.type_shift as type11_0_0_, user0_.user_group as user12_0_0_ from tbluser user0_ where user0_.user_id=?|#]

seems that UserShift query is not there

Kindly please enlight me how to solve this


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 07, 2006 6:46 am 
Regular
Regular

Joined: Wed Mar 23, 2005 8:43 am
Posts: 105
Location: Moscow, Russia
You trying to use property of the lazily fetched association: u.getTypeshift().getDesc() on the client.
At this moment you have only proxy, and you can't fetch this associatiation lazily on the client, only on server in hibernate session. That's why you recive an exception. You must fetch this association eagerly (using fetch join for example) to work with association on the clent.

_________________
Best Regards


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 07, 2006 8:11 am 
Newbie

Joined: Tue Oct 17, 2006 5:18 am
Posts: 9
Thanks Lester for the reply.
It solve the problem, but it wont work if I retrieve object from List type, the exception still remain there

AdminFacadeRemote afs = (AdminFacadeRemote)lookupbeanAdmin().create(AppConfig.ADMINUSER);
Iterator it = afs.listUser().iterator();
while (it.hasNext()) {
User mt = (User) it.next();
TypeShift ts = mt.getTypeshift();
System.out.println(mt.getFullname());
System.out.println(ts.getDesc()); //this line will give me LazyInitializationException
}


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 07, 2006 8:35 am 
Regular
Regular

Joined: Wed Mar 23, 2005 8:43 am
Posts: 105
Location: Moscow, Russia
How i understand, now you have method that returns List of User objects. Did you change the mapping? Can i look at new mapping if it is and at method that returns List of User objects?

_________________
Best Regards


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 07, 2006 8:49 am 
Newbie

Joined: Tue Oct 17, 2006 5:18 am
Posts: 9
Your first suggestion does solve the problem for my first method the
public User getUser(String username)

But when I try with my other method that return List I got those LazyInitializationException again on the ejb client

this are my code

public List listUser() {
List lstuser = null;
try {
HibernateUtil.beginTransaction();
Session lses = HibernateUtil.getSession();
Query query = lses.createQuery("from User");
lstuser = query.list();
HibernateUtil.closeSession();
} catch (Exception e) {
HibernateUtil.rollbackTransaction();
e.printStackTrace();
}
return lstuser;
}

the mapping still the same,

Thanks again for the quck reply


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 07, 2006 9:12 am 
Regular
Regular

Joined: Wed Mar 23, 2005 8:43 am
Posts: 105
Location: Moscow, Russia
How i understand, the problem is th same - you recive collection of User obects with uninitialiazed TypeShifts. Try to use any of following variants:

1. Use HQL: from User o fetch join TypeShift (or, if you prefer, yo may use Criteria API with FetchMode.EAGER)
2. Use Hibernate.initialize(user.getTypeShift()) for each User on server.

_________________
Best Regards


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 08, 2006 12:21 am 
Newbie

Joined: Tue Oct 17, 2006 5:18 am
Posts: 9
Thank so much Lester, this really solve my problem


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.