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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With