*
The moose likes EJB and other Java EE Technologies and the fly likes LazyInitializationException and transaction Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "LazyInitializationException and transaction" Watch "LazyInitializationException and transaction" New topic
Author

LazyInitializationException and transaction

Gabriel Vargas
Ranch Hand

Joined: May 16, 2007
Posts: 145
Hi.

I get an LazyInitializationException when i try to use an entity bean (ejb3) in a backing bean (presentation layer) (jsf), because the entity bean is out of transaction defined in my business layer. How can i use my entity bean in my presentation layer?


Gabriel Vargas
SCJP, SCJD, now studying for SCWCD and working to be a better person
Sunil Dixit
Ranch Hand

Joined: Sep 22, 2005
Posts: 46
Hi,
Do you entity bean have any relationship with other bean ?
If yes and you are trying to fetch data using your parent entity,than you shd use aggressive fetching stg.
If you business say that you can't have aggressive fetching stg. than you need to populate the required data in you parent bean.
e.g.
Student -> Course
(1) (N)
So in student bean either you mark fetching stg as aggressive OR write necessary code in StudentBean to fetch the required data.
The exception which your talking abt is occured when you try to access something whose proxy is not initialise.
Hope that this will help you.

Cheers,
Sunil Dixit
Gabriel Vargas
Ranch Hand

Joined: May 16, 2007
Posts: 145
Hi Sunil,

I try to put both aggresive and lazy strategies but its fails .I try to explain better. I have sessions beans than acts as facades to all my business services, these beans manages transactionality. These beans are called from jsf backing beans. The problem occur when i try to get an entity bean and use in a backing bean then i get this exception. I try to put transactionality in backing beans and it works, but it seems not good to me because i am mixing business layer and presentation layer, i want to keep transactionality in my business layer (session beans) and use results of entity beans in my backing beans. I'm usign JBoss and hibernate. Any idea how do this?
Sunil Dixit
Ranch Hand

Joined: Sep 22, 2005
Posts: 46
Hi,
Thanks for giving this information,could you please post the piece of code which is used to get the data from the database.The reason why I am asking I need to have a look how you are fethcing the data from database using Hibernate.
Let me tell you how Hibernate works, when you try to load and object using Hibernate than your POJO class is being enhanced by Hibernate framework to PROXY which know how to get/set the details from database.This PROXY will work fine till the time you have vaild Tx/Session.
The time you close you Tx/Session than this PROXY is not valid,If you want to use your POJO object outside of Tx context and try to get some information which was not loaded during PROXY had valid Tx/context than you will get same exception.
So I want to have a look how you are usning Hibernate to get the information.
After seeing that I can tell you something.

Cheers,
Sunil Dixit
Gabriel Vargas
Ranch Hand

Joined: May 16, 2007
Posts: 145
Hi Sunil.

First i show an entity, this is managed by JBoss and Hibernate with EJB3 spec.

/

As you can see is necesary construct a business object to manage object out of transaction defined in my facade session bean. My facade session bean looks like.



And my jsf backing bean looks like:



My entity bean called Car is used bellow my session bean because transaction is defined in this level, if i try to use that in my backing bean i get that exception, the only solution than i can find is put transaction in my backing bean but it seems not good to me or use the busines object as show in example , i think there is another way to do this better.
[ July 10, 2007: Message edited by: Juan Gabriel Vargas Bola�os ]
Gabriel Vargas
Ranch Hand

Joined: May 16, 2007
Posts: 145
Hi, somebody can help me
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: LazyInitializationException and transaction
 
Similar Threads
LazyInitializationException
LazyInitializationException in Spring
LazyInitializationException
LazyInitializationException
LazyInitializationException - Hibernate