HibernateTransactionManager或JpaTransactionManager [英] HibernateTransactionManager or JpaTransactionManager

查看:113
本文介绍了HibernateTransactionManager或JpaTransactionManager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与朋友一起开发应用程序,但碰到一个问题...
我有这样的代码:

 << ; bean id =sessionFactory
class =org.springframework.orm.hibernate4.LocalSessionFactoryBean>
< property name =dataSourceref =dataSource/>
< property name =configLocation>
< value> classpath:hibernate.cfg.xml< / value>
< / property>
< property name =hibernateProperties>
<道具>
< prop key =hibernate.dialect> $ {jdbc.dialect}< / prop>
< prop key =hibernate.show_sql> true< / prop>
< prop key =hibernate.hbm2ddl.auto>建立< / prop>
< /道具>
< / property>
< / bean>

< tx:annotation-driven />
< bean id =transactionManager
class =org.springframework.orm.hibernate4.HibernateTransactionManager>
< property name =dataSourceref =dataSource/>
< property name =sessionFactoryref =sessionFactory/>
< / bean>

然后他将其全部删除,并将其更改为:

 < bean id =transactionManagerclass =org.springframework.orm.jpa.JpaTransactionManager> 
< property name =entityManagerFactoryref =entityManagerFactory/>
< / bean>

< tx:注解驱动的事务管理器=transactionManager/>
< bean id =entityManagerFactoryclass =org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean>
< property name =persistenceUnitNamevalue =persistenceUnit/>
< property name =dataSourceref =dataSource/>
< / bean>

他说这是更好的方法,但并没有完全解释原因。关于不直接使用hibernate的事情,这是否意味着我们不再使用hibernate了?



DAO变成这样:



我有:

  @Autowired 
private SessionFactory sessionFactory;
$ b $ public void addUser(User user){
sessionFactory.getCurrentSession()。save(user);

$ / code>

现在是:

  @PersistenceContext 
private EntityManager entityManager;

public void addUser(User user){
entityManager.persist(user);


解决方案

他希望你的代码是依赖于JPA,而不是Hibernate。 javax.persistence.EntityManager 是一个JPA(Java EE)标准接口,而 org.hibernate.SessionFactory 是一个Hibernate有用的界面。从SessionFactory移动到EntityManager使您的类不再依赖于Hibernate。使用JPA-way而不是Hibernate-way被大多数人认为是现今最好的做法。

请注意,您的系统仍然依赖于Hibernate,因为您需要JPA提供商。但是如果你想在未来转换到另一个JPA提供者,它应该是非常简单的。去JPA的另一个好处是JPA接口比Hibernate接口更稳定。


Developing application with friend, but ran into a question... I had this code :

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="configLocation">
        <value>classpath:hibernate.cfg.xml</value>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">${jdbc.dialect}</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.hbm2ddl.auto">create</prop>
        </props>
    </property>
</bean>

<tx:annotation-driven />
<bean id="transactionManager"
    class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="dataSource" ref="dataSource" />
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

And he removed it all, changed it to:

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

<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceUnitName" value="persistenceUnit"/>
    <property name="dataSource" ref="dataSource"/>
</bean>

He said, that this is better approach, but did not exactly explain why. Something about not using hibernate directly, does this mean we are not using hibernate at all anymore? Is it really better approach?

DAO changed like this:

I had:

@Autowired
private SessionFactory sessionFactory;

public void addUser(User user) {
    sessionFactory.getCurrentSession().save(user);
}

Now is:

@PersistenceContext
private EntityManager entityManager;

public void addUser(User user) {
    entityManager.persist(user);
}

解决方案

He wants your code to be dependent on JPA, instead of Hibernate. javax.persistence.EntityManager is a JPA (Java EE) standard interface, while org.hibernate.SessionFactory is a Hibernate propertary interface. Moving from SessionFactory to EntityManager makes your classes no longer dependent on Hibernate. Using the JPA-way instead of the Hibernate-way is considered best practice by most people today.

Please note that your system is still dependent upon Hibernate, as you need a JPA provider. But if you want to change to another JPA provider in the future it should be pretty straight forward. Another advantage for going for JPA is that the JPA-interfaces are more stable than the Hibernate ones.

这篇关于HibernateTransactionManager或JpaTransactionManager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆