使用spring的hibernate的正确方法是什么?

Mik*_*ike 2 integration spring hibernate

我最近开始使用hibernate和spring.

在开始时,我被告知使用sessionFactory和openSession以及beginTransaction进行数据库调用.

后来我听说dao的,所以我开始使用它创建一个接口,在类中实现这个接口,然后让这个类扩展HibernateDAOSupport.

我认为这是非常可靠的,直到一位同事告诉我这种方式已被弃用,我不应该使用HibernateDAOSupport类.但是在我的dao实现类中使用SessionFactory实例,在新的单独服务类中使用该类的实例.由于这似乎是一个很好的方法,我决定遵循这条道路.

刚才我已经读过这个方法也被新版本的春天所弃用......所以我的问题是:究竟什么是将hibernate和spring结合在一起的正确最新方法?

我也听过关于实体经理的讨论,那是什么意思?

我正在寻找使用它们的一般方法,如果一般规则有任何例外,请提供这些例外的示例.

lim*_*imc 5

这是将Hibernate集成到Spring中的众多方法之一......

您的服务(或DAO)类应该看起来像这样简单: -

// annotate this class so that Spring is aware of it 
@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private SessionFactory sessionFactory;

    // this is straight-up HQL... really, no magic here
    @Override
    @SuppressWarnings("unchecked")
    public Collection<Employee> getAllEmployees() {
        return sessionFactory.getCurrentSession()
                .createQuery("from Employee e order by e.lastName, e.firstName")
                .list();
    }
}
Run Code Online (Sandbox Code Playgroud)

现在,您需要配置sessionFactory,否则自动装配将失败.那么,这是你如何配置它: -

<?xml version="1.0" encoding="UTF-8"?>
<beans  xmlns="http://www.springframework.org/schema/beans" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:jee="http://www.springframework.org/schema/jee"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
            http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd">

    <!-- 
        When creating the session factory, point it to your existing hibernate.cfg.xml.
        You can also port your entire Hibernate configuration and HBM mappings here, but
        for simplicity sake, I'll reference the existing hibernate.cfg.xml here so that we 
        are not cluttering Spring configuration file with Hibernate-specific configuration.
    -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="configLocation" value="classpath:hibernate.cfg.xml"/>
    </bean>

    <!-- If you are running in production, you will want to use JNDI connection -->
    <jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/jdbc/myapp"/>

    <!-- If you are running testcases, you might want to use JDBC instead -->
    <!--
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="net.sourceforge.jtds.jdbc.Driver"/>
        <property name="url" value="jdbc:jtds:sqlserver://machine/myapp"/>
        <property name="username" value="myapp"/>
        <property name="password" value="myapp"/>
    </bean>
    -->

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

    <tx:advice id="txAdvice">
        <tx:attributes>
            <tx:method name="*" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>

    <aop:config proxy-target-class="true">
        <aop:advisor pointcut="execution(* com.myapp..*.*(..))" advice-ref="txAdvice"/>
    </aop:config>

</beans>
Run Code Online (Sandbox Code Playgroud)

配置事务有两种方法:1)使用@Transactional,您可以注释需要正确处理事务的类(或特定方法),以及2)使用AOP并使用事务包装所有代码.

在我上面的例子中,我将所有代码都包装在com.myapp带有事务的基础包中.您可以更改(或添加)切入点以减少事务包装器.