4

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 3

8

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

Sign up to request clarification or add additional context in comments.

Comments

2

You can change the fetching strategy using Fetching Profiles.

2 Comments

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().
You can find additional information related to fetching at docs.jboss.org/hibernate/core/3.6/reference/en-US/html_single/….
-1

Yes you can.
The full details are here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.