4

I am using spring + hibernate in my project; I have two classes Reminder and Client in class reminder i have added a relationship of many to one for client and it is by default eagerly loaded. I want this Object graph most of the scenarios in my project so i have set fetch type eager for client in reminder class

Class Reminder {
    @ManyToOne
    Client client;
}

but for one or two scenarios i want to keep initialization of this object client lazy;

so i have added in method for fetching reminders is like below

Criteria c = session.createCriteria();
c.setFetchMode("client", FetchMode.SELECT); 
hibernateTemplate.findByCriteria(criteria);

it is not working; it still loads client objects with reminder

while reverse (from lazy to eager) is working fine

Tomasz Błachowicz
  • 5,731
  • 9
  • 41
  • 47
jignasha
  • 73
  • 1
  • 8

2 Answers2

2

From the api doc:

public static final FetchMode SELECT

Fetch eagerly, using a separate select. Equivalent to fetch="select"

AFAIK, if a mapping is marked as lazy, you may fetch eagerly using a criteria or HQL query, but you can't do the reverse : if a mapping is marked as eager, then it'll always be fetched eagerly.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
0

I think you can't have lazy loading on single ended association that can be null(many-to-one, one-to-one). Hibernate3 supports lazy loading of individual fields using some byte code stuff.

From JBoss wiki:

Use lazy="true" on , and mappings to enable lazy loading of individual scalar value-typed properties (a somewhat exotic case). Requires bytecode instrumentation of compiled persistent classes for the injection of interception code. Can be overriden in HQL with FETCH ALL PROPERTIES.

Use lazy="no-proxy" on single-valued associations to enable lazy fetching without the use of a proxy. Requires bytecode instrumentation for the injection of interception code.

Thanks.

Nikunj
  • 3,100
  • 2
  • 20
  • 19
  • Thank you, but it is not for oneToMany associations also;i tried it; it's not working and can u please give me sample code for byte code stuff for lazy initialization – jignasha Jun 16 '11 at 11:02
  • Check out this link from [JBoss](http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/performance.html#performance-fetching-lazyproperties). – Nikunj Jun 16 '11 at 11:17