I have an entity in which i have specified lazy="false" and batch-size="100". It is working fine but in some other scenario i want to remove batch -size and set lazy="true". If i change hbm files then it affect other applications. Is there any way i can change properties of entity for current session only before executing hql.
3 Answers
You can change the fetching strategy (lazy or not) at runtime by HQL or criteria query. In HQL your can use fetch join to initialize values of a joined collection, example:
from Cat as cat
inner join fetch cat.mate
left join fetch cat.kittens
See Hibernate Doku - 15.3. Associations and joins
Use Criteria.setFetchMode(..) of criteria api instead for criteria queries, example:
List cats = sess.createCriteria(Cat.class)
.add( Restrictions.like("name", "Fritz%") )
.setFetchMode("mate", FetchMode.EAGER)
.setFetchMode("kittens", FetchMode.EAGER)
.list();
Hibernate Doku for this: 16.5. Dynamic association fetching
Comments
You can change the fetching strategy using Fetching Profiles.
2 Comments
snorbi
As I know there is no way to change a batch size dynamically. You can probably change it at configuration time by modifying the Configuration object used to create the SessionFactory. See org.hibernate.cfg.Configuration.getClasses() and org.hibernate.mapping.Collection.setBatchSize().
snorbi
You can find additional information related to fetching at docs.jboss.org/hibernate/core/3.6/reference/en-US/html_single/….