How to change hibernate display properties at runtime

I have an object in which I specified lazy = "false" and batch size = "100". It works fine, but in another scenario I want to remove the batch size and set lazy = "true". If I change hbm files, it affects other applications. Is there a way to change the properties of an object for the current session just before hql is executed.

+4
source share
3 answers

You can change the selection strategy (lazy or not) at run time by HQL query or criteria. In HQL, you can use fetch join to initialize the values ​​of a merged collection, for example:

from Cat as cat inner join fetch cat.mate left join fetch cat.kittens 

See Hibernate Doku - 15.3. Associations and Associations

Use Criteria.setFetchMode (..) of api criteria instead of querying criteria, for 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 Highlighting

+8
source

You can change the selection strategy using Extract Profiles .

+2
source

Yes, you can.
Full details here .

0
source

Source: https://habr.com/ru/post/1334185/


All Articles