-->
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.  [ 5 posts ] 
Author Message
 Post subject: Turn off lazy property loading
PostPosted: Fri Feb 24, 2006 1:33 am 
Regular
Regular

Joined: Thu Jul 29, 2004 11:55 pm
Posts: 75
Hibernate 3.1.1 annotations 3.1beta8

Hi,

I am having an issue were my properties are being lazy loaded by default. I don't want lazy properties. How do I have my properties default to non-lazy unless I specify @Basic(fetch = FetchType.LAZY).

chuck


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 27, 2006 4:52 am 
Newbie

Joined: Tue Jan 10, 2006 11:59 am
Posts: 17
1. First of all it is not good idea to trun it off by default, because if you have, for example, entity with "parent" and "children" properties, hibernate will got OutOfMemoryException when try to read ALL parents and childre.

2. If you use your own loader for hibernate (not J2EE), then you can iterate through all entity beans and set fetch type to FETCH in hibernate properties before creating session factory.

_________________
--
Sergey Vladimirov


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 01, 2006 11:58 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Collections are lazy loaded by default which is good

manytoone and onetoone association are not lazy loaded by default but they should

properties are not lazy loaded by default which is good

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 07, 2006 7:02 am 
Newbie

Joined: Fri Apr 07, 2006 3:50 am
Posts: 3
I must be doing something wrong, because I really think that properties are lazy loaded.
Using Hibernate 3.2 CR1 with Annotations 3.1 beta9, properties on a normal entity are loaded only when I first access any of them.

Example:
Code:
@Table(name = "semantic_item")
@Entity
public class SemanticItem implements Serializable {
   
   @Id
   @GeneratedValue(strategy = GenerationType.AUTO)
   private Integer id;
   @Column(name="CONTENT_MEDIA_ID")
   private Integer contentMediaId;
   @Column(name="NAME_KEY")
   private String nameKey;
   
   public SemanticItem() {}

   //getters and setters
}


I load the class using:
Code:
     SemanticItem semanticItem = (SemanticItem) session.load(SemanticItem.class, id)


If I close the session, and try to access any property of the object afterwards (without having done it before), I get a LazyInitializationException.

¿Maybe I am overriding the defaults somehow? ¿Is there any way to explicitly specify eager loading for all properties (without iterating through all of them)?

thanks


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 07, 2006 7:19 am 
Newbie

Joined: Tue Jan 10, 2006 11:59 am
Posts: 17
There is misunderstanding of load() method behaviour. This method does not load entity from database at all.

If entity was not loaded before, this method returns special proxy object. This object will load himself at first time when you access any property.

There are two ways to solve problem:

1. Access any entity property BEFORE you close the session. So proxy object will load all properties at this moment.

2. Load object with get()/find() methods, not by load() method. Then it will be loaded instead of proxy.

_________________
--
Sergey Vladimirov


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