简体   繁体   中英

what is the use of Hibernate Interceptor and proxyTargetClass

I am a newbie to hibernate and spring and trying to learn the use of hibernate Interceptor . I went through community documentation which says that....

This interceptor binds a new Hibernate Session to the thread before a method call, closing and removing it afterwards in case of any method outcome. If there already is a pre-bound Session (eg from HibernateTransactionManager, or from a surrounding Hibernate-intercepted method), the interceptor simply participates in it.

But I am not able to understand when we use this and why we use this?

when to create taskDao like this ?

<bean name="abstractDao" abstract="true">
        <property name="hibernateTemplate" ref="taskHibernateTemplate"/>
    </bean>
<bean id="taskHibernateInterceptor" class="org.springframework.orm.hibernate3.HibernateInterceptor">
        <property name="sessionFactory" ref="taskSessionFactory"/>
    </bean>

<bean id="taskDaoTarget" class="com.xyz.abc.backgroundtask.impl.TaskDao" parent="abstractDao"/>

<bean id="taskDao" class="org.springframework.aop.framework.ProxyFactoryBean">
    <property name="proxyTargetClass" value="true"/>
    <property name="interceptorNames">
        <list>
            <value>taskHibernateInterceptor</value>
            <value>taskDaoTarget</value>
        </list>
    </property>
</bean>

and when to create taskDao like this ?

<bean name="abstractDao" abstract="true">
        <property name="hibernateTemplate" ref="taskHibernateTemplate"/>
    </bean>

<bean id="taskDao" class="com.xyz.zbc.backgroundtask.impl.TaskDao" parent="abstractDao"/>

Do you have to use Hibernate Interceptor? Because I'd suggest you use Spring's (annotation based) Declarative Transaction Management instead. It's a common abstraction for many supported underlying technologies.

Basically, what you do is define a TransactionManager Bean, in the case of Hibernate with JPA:

<bean id="transactionManager"
    class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="myEmf"/>
</bean>

without JPA:

<bean id="transactionManager" 
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"/>
</bean>

In both cases, activate annotation-based transactions:

<tx:annotation-driven />

Or, if you don't use interfaces:

<tx:annotation-driven proxy-target-class="true" />

Now annotate your service layer methods or classes with @Transactional and you will automatically get sessions created in the scope of your service methods.

I would advise you not to use HibernateInterceptor and HibernateTemplate, they are both just not the modern way to do things anymore.

Read:

And to see how things used to be done in Spring:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM