dynamically lazy loading of objects does not work
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开发者_JAVA百科 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
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.
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.
精彩评论