I set lazy property as "true" in my hbm mapping file. However I want to change "lazy=false" dynamically through code. how can I achieve this?
2 Answers
You can use either joins via HQL or FetchMode.JOIN via Criteria API, in this case you'll get your associations in a single shot. Note, that associated objects won't be fetched from 2nd level cache if you use it.
Another option is Hibernate.initialize() or simply just invoke object.getAssociation().
Comments
Look at sessionFactory.getClassMetadata(YourClass.class). It has the methods to access every field.
The more civilized way, however, is to override fetching strategy with HQL or Criteria query. Another option is to modify your metadata (having lazy="false") and rebuild the session factory. It's not that scary as it looks like.