-->
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: LazyInitializationException when session.evict
PostPosted: Wed Mar 24, 2004 7:16 pm 
Regular
Regular

Joined: Sun Oct 26, 2003 9:02 pm
Posts: 90
I'm using jdk1.3.1_09 with Hibernate 2.1.2.

I'm getting the following exception when I'm working with a collection were the parent object has been evicted.

LazyInitializationException:? - Failed to lazily initialize a collection - no session or session was closed

The problem with this is that if I'm going to work with lazy initialization I can't evict the objects. Lazy Initialization is important so I can partially load objects and not all their relations with other objects.

I need to evict my objects so Hibernate doesn't serialize the objects in a flush operation if they have been modified. This is also important when you're working with Oracle CLOBs and BLOBs.

Isn't there a way that I can evict my objects and use LazyInitialization???
or
Is there a way that I can disable the serialization of modified objects when the flush operation occurs???


Top
 Profile  
 
 Post subject: Repost
PostPosted: Thu Mar 25, 2004 10:36 pm 
Regular
Regular

Joined: Sun Oct 26, 2003 9:02 pm
Posts: 90
repost


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 26, 2004 4:26 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Please be more specific with your description (a smalll piece of code is better than long english speak).

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Example code
PostPosted: Fri Mar 26, 2004 10:15 am 
Regular
Regular

Joined: Sun Oct 26, 2003 9:02 pm
Posts: 90
Hi emmanuel

Thanks for the reply. Here is the example code:

SQL:

create table parent (id number (20) primary key, name varchar2 (255))
create table child (id number (20) primary key, name varchar2 (255), parent_id number (20));
create table sequences (next_value number (20));

Parent.java

public class Parent
{
protected long id;
protected String name;
protected List childs;

public Parent()
{
}

public long getId()
{
return id;
}

public void setId(long newId)
{
id = newId;
}

public String getName()
{
return name;
}

public void setName(String newName)
{
name = newName;
}

public List getChilds()
{
return childs;
}

public void setChilds(List newChilds)
{
childs = newChilds;
}
}

Child.java

public class Child
{
protected long id;
protected String name;

public Child()
{
}

public long getId()
{
return id;
}

public void setId(long newId)
{
id = newId;
}

public String getName()
{
return name;
}

public void setName(String newName)
{
name = newName;
}
}

Test.java

Transaction transaction;
Configuration configuration = new Configuration ();
SessionFactory factory;
Session session;
Parent parent;
Child child;

configuration.addClass (Parent.class);
configuration.addClass (Child.class);

factory = configuration.buildSessionFactory ();
session = factory.openSession ();

parent = new Parent ();
parent.setName ("Example");

transaction = session.beginTransaction ();
session.saveOrUpdate (parent);
transaction.commit ();
session.evict (parent);

parent = (Parent) session.find ("from Parent where name = ?", "Example", Hibernate.STRING).get (0);
session.evict (parent);

child = new Child ();
child.setName ("Example Child");

parent.getChilds ().add (child); // Here it throws the Exception


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 26, 2004 3:39 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
This is expected, the collection has not been init and you detached the object, so Hibernate can't load the collection.
Either, reattach the parent object, init the colelction before evicting it.

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Workaround
PostPosted: Fri Mar 26, 2004 4:11 pm 
Regular
Regular

Joined: Sun Oct 26, 2003 9:02 pm
Posts: 90
Emmanuel

Thanks for the quick reply.

The workarounds that you provide are very good but I can't use them in my project because the interface can change this objects without accesing the Hibernate session.

So the only way I can use LazyInitialization is to eliminate the session.evict line but I can't because Hibernate will serialize any updated objects in the session cache in a flush operation.

Do you know by any chance if I can disable this option?

Thanks for the help


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 27, 2004 5:03 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
session.setFlushMode(FlushMode.NEVER);

_________________
Emmanuel


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.