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

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks for replying; but i found on net that api doc for FetchMode.Select is wrong; it fetches lazily
The doc is correct, fetching means how we load data where are lazy true/false is whether or not we load the data.
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.

2 Comments

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
Check out this link from JBoss.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.