Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change hibernate mapping properties at runtime

Tags:

hibernate

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.

like image 506
dmay Avatar asked Jan 21 '23 19:01

dmay


1 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

like image 189
uı6ʎɹnɯ ꞁəıuɐp Avatar answered Jan 26 '23 11:01

uı6ʎɹnɯ ꞁəıuɐp