TrainBeaser for iPhone
The moose likes Object Relational Mapping and the fly likes Lazy initialization exception Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Object Relational Mapping
Reply Bookmark "Lazy initialization exception" Watch "Lazy initialization exception" New topic
Author

Lazy initialization exception

Rafael Andrade
Greenhorn

Joined: Aug 04, 2006
Posts: 15
Hi there. I'm having an annoying problem. I got a mapped property on my "boleto" class, and it's working well while saving the object in the db:

@ManyToOne (targetEntity = Pessoa.class, fetch = FetchType.LAZY)
@JoinColumn (name = "ID_PESSOA_SACADO", nullable=false)
private Pessoa sacado; ... getters and setters...

when I get the object from the db, it comes without the "sacado", of course, because it's lazy. When I try to load it in the DAO like this:

boleto = (BoletoVO) sess.merge(boleto);
Hibernate.initialize(boleto.getSacado());

it doesn't bring the object "sacado", and when I click on it on the Debug mode, it gives me:

org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed

the id of the "sacado" property is allright in the db.

when I run this HQL:
select bol.sacado from BoletoVO bol join bol.taxa join bol.sacado where...
it returns the right object. What is happening? thanks!
Arun Kumarr
Ranch Hand

Joined: May 16, 2005
Posts: 504

try changing you entry to "@ManyToOne( cascade = {CascadeType.MERGE}, targetEntity = Pessoa.class, fetch = FetchType.LAZY)
[ August 01, 2007: Message edited by: Arun Kumarr ]

If you are not laughing at yourself, then you just didn't get the joke.
 
 
subject: Lazy initialization exception
 
Threads others viewed
What does Lazy replication and lazy loading means ?
Hibernate one-to-many weirdness...
hibernate left join fetch question
Setting lazy For One-to-One relation using Configuration Object
lazy loading of subclass in hibernate
IntelliJ Java IDE