-->
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.  [ 11 posts ] 
Author Message
 Post subject: LAZY, LAZY
PostPosted: Tue Nov 01, 2005 3:24 pm 
Senior
Senior

Joined: Tue Sep 13, 2005 2:01 am
Posts: 137
How to load LAZY entities?
and how to make a property LAZY?
----------

Entity A has a reference to Entity B. (OneToOne)

@Entity
public class A {
// id ...

@OneToOne(fetch=FetchType.LAZY)
public B getB() {
return b;
}

public void setB(B b) {
this.b = b;
}


}

A a = aSessionBean.getA(id);
...

B b = a.getB();

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

when is the owning session created and closed?
What is the right way to access Lazy property(such as B above)?



-------

I tried to tag a property as LAZY using
@LOB
@Basic(fetch=FetchType.LAZY)
public String getDescription() {
...
}

it did not work either. It is being treated as EAGER from generated SQL.


Thank you for your help. Have a nice day.

Dave


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 02, 2005 6:55 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
1) You're using JBoss EJB3 right? so at your tx demarkation the session is opened and closed. You have to load the object within this tx either by accessing the object state or by using an ejbql fetch

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 02, 2005 6:55 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
2) Use the build time enhancement task as stated in the doc

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 02, 2005 7:09 pm 
Senior
Senior

Joined: Tue Sep 13, 2005 2:01 am
Posts: 137
emmanuel wrote:
1) You're using JBoss EJB3 right? so at your tx demarkation the session is opened and closed. You have to load the object within this tx either by accessing the object state or by using an ejbql fetch


I am using EJB3. It is not useful if I need to load the lazy entity in the same transaction. For web application, show some small data first, then show lazy(huge)data when user request it. they are in two transactions. If I use ejbql to load lazy data, will Hibernate build object relationship (navigation) that is defined using annotation such as @OneToMany ....


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 02, 2005 7:14 pm 
Senior
Senior

Joined: Tue Sep 13, 2005 2:01 am
Posts: 137
emmanuel wrote:
2) Use the build time enhancement task as stated in the doc

I am not quite understanding. I could not find doc related. A little more help is appreciated. thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 03, 2005 7:20 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
javatwo wrote:
emmanuel wrote:
2) Use the build time enhancement task as stated in the doc

I am not quite understanding. I could not find doc related. A little more help is appreciated. thanks.


HA
Quote:
The detailedComment property value will be lazily fetched from the database once a lazy property of the entity is accessed for the first time. The compiled code of your class has to be instrumented for this last feature, please refer to the Hibernate reference documentation.


and http://www.hibernate.org/hib_docs/v3/reference/en/html_single/#performance-fetching-lazyproperties

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 03, 2005 7:23 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
javatwo wrote:
I am using EJB3. It is not useful if I need to load the lazy entity in the same transaction. For web application, show some small data first, then show lazy(huge)data when user request it.

If you don't, then the ACID properties of your data will be lost which is *very* bad. To resume a work on an object graph wo loosing ACIDity, you can reattach it through the merge operation and use the optimistic locking (@Version).

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 16, 2005 1:04 am 
Senior
Senior

Joined: Tue Sep 13, 2005 2:01 am
Posts: 137
emmanuel wrote:
javatwo wrote:
emmanuel wrote:
2) Use the build time enhancement task as stated in the doc

I am not quite understanding. I could not find doc related. A little more help is appreciated. thanks.


HA
Quote:
The detailedComment property value will be lazily fetched from the database once a lazy property of the entity is accessed for the first time. The compiled code of your class has to be instrumented for this last feature, please refer to the Hibernate reference documentation.


and http://www.hibernate.org/hib_docs/v3/reference/en/html_single/#performance-fetching-lazyproperties



code instrumentation is not a clean solution. My understanding is: If a property is declared to lazy, just do not include it in the select statement. It should be very simple. I do not like the way of instrumenting code. I think this is a bug. Delared to be lazy, but not lazy.

I have a class that has a Lob property that I like to be lazy. I can use query to select all properties except the lob. It is lazy. When I need the lob data, ask EM to load it. The problem is that, Hibernate does not support it in the case of association with other entities. For example,


class A {

@OneToMany(fetch=FetchType.EAGER)
public List<B> getBs();

...
}


class B {

@Basic(fetch=FetchType.Lazy)
@Lob(type=LobType.BLOB)
public getData();

}


When A is loaded, all the Bs will be loaded (eager). I like B's data not to be loaded. However Hibernate ignores Lazy.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 16, 2005 1:47 am 
Senior
Senior

Joined: Tue Sep 13, 2005 2:01 am
Posts: 137
One more question:

class A {

@ManyToMany(fetch=FetchType.LAZY)
@JoinTable (....)
public List<B> getBs();

...
}

In this case, it is lazy. Hibernate will not load Bs when loading A.

How to load Bs using EntityManager? I am not supposed to query JoinTable, right? Can EJB QL query join tables?

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 16, 2005 6:49 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
javatwo wrote:
code instrumentation is not a clean solution. My understanding is: If a property is declared to lazy, just do not include it in the select statement. It should be very simple.

This is exactly what is done! But how do I know when you access a lazy property and that a second select has to be executed? By using byte code enhancement. The select and the bytecode enhancement are 2 orthogonal notions.

javatwo wrote:
I do not like the way of instrumenting code. I think this is a bug. Delared to be lazy, but not lazy.

Wrong see above.

Quote:
I have a class that has a Lob property that I like to be lazy. I can use query to select all properties except the lob. It is lazy. When I need the lob data, ask EM to load it. The problem is that, Hibernate does not support it in the case of association with other entities. For example,


class A {

@OneToMany(fetch=FetchType.EAGER)
public List<B> getBs();

...
}


class B {

@Basic(fetch=FetchType.Lazy)
@Lob(type=LobType.BLOB)
public getData();

}


When A is loaded, all the Bs will be loaded (eager). I like B's data not to be loaded. However Hibernate ignores Lazy.


2 mistakes here.
@Lob(type=LobType.BLOB, fetch=FetchType.LAZY)
@Basic is not compatible with @Lob

Second a Blob if retrieved in a java.sql.Blob is already lazy as per the JDBC spec.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 16, 2005 7:13 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
javatwo wrote:
One more question:

class A {

@ManyToMany(fetch=FetchType.LAZY)
@JoinTable (....)
public List<B> getBs();

...
}

In this case, it is lazy. Hibernate will not load Bs when loading A.

How to load Bs using EntityManager? I am not supposed to query JoinTable, right? Can EJB QL query join tables?

Thanks.


1. through navigation
2. using an ejb-ql query with the fetch keyword
select a from A a fetch join a.bs
3. by setting FetchType.EAGER

Please read the doc, this is explained.

_________________
Emmanuel


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 11 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.