开发者

what is the use of Hibernate Interceptor and proxyTargetClass

开发者 https://www.devze.com 2023-04-03 08:31 出处:网络
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....

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 (e.g. 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>开发者_JS百科
</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:

  • Declarative Transaction Management
  • Using @Transactional
  • Implementing DAOs based on plain Hibernate 3 API
  • or if you use JPA: Implementing DAOs based on plain JPA

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

  • Classic ORM Usage: The HibernateTemplate
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号