Java
Spring, Hibernate and Annotations
1. Place
the following in your application-context.xml
<bean id="openSessionInViewInterceptor"
class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor"
>
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
<property name="flushModeName">
<value>FLUSH_AUTO</value>
</property>
</bean>
2. Add
The Interceptor to your interceptor list
<property name="interceptors">
<list>
<ref bean="openSessionInViewInterceptor" />
</list>
</property>
3. Add
FetchType.LAZY to the annotation mappings you need to lazy load.
@ManyToMany(cascade
= { CascadeType.PERSIST,CascadeType.MERGE },fetch=FetchType.LAZY)
4. In
instances you need to load eagerly things you have set to lazy.
Use the following
example.
Criteria
crit = this.hibernateTemplate.getSessionFactory()
.getCurrentSession()
.createCriteria(Test.class)
.add(Restrictions.eq("id",id))
.setFetchMode("subTest",
FetchMode.JOIN)
.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
return (Indicator)crit.uniqueResult();
|