Hibernate4.1.8 整合Spring,Spring 3.1.2管理事务 
请问Dao中是用什么方法拿SessionFactory 呢? 
继承Hibernate HibernateDaoSupport 没用因为 Spring只支持hibernate3 后面就不支持了

解决方案 »

  1.   

    dao中直接使用sessionfactory啊,我给你一段注解的
    @Autowired
    @Qualifier("sessionFactory")
    private SessionFactory sessionFactory;    public Session getSession() {
            //事务必须是开启的(Required),否则获取不到
            return sessionFactory.getCurrentSession();
        }
    applicationcontext.xml中
        <bean id="sessionFactory"
    class="org.springframework.orm.hibernate4.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="hibernateProperties">
    <props>
    <prop key="hibernate.dialect">${database.dialect}</prop>
    <prop key="hibernate.hbm2ddl.auto">update</prop>
               
    </props>
    </property>
    <property name="packagesToScan">
    <list>
    <value>com/test/demo/bean</value><!-- 扫描实体类,也就是平时所说的model -->
    </list>
       </property>
    </bean>
    <tx:annotation-driven transaction-manager="txManager"/>    <bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
            <property name="sessionFactory" ref="sessionFactory"/>
        </bean>    <tx:advice id="txAdvice" transaction-manager="txManager">
            <tx:attributes>
                <tx:method name="save*" propagation="REQUIRED" />
                <tx:method name="add*" propagation="REQUIRED" />
                <tx:method name="create*" propagation="REQUIRED" />
                <tx:method name="insert*" propagation="REQUIRED" />
                <tx:method name="update*" propagation="REQUIRED" />
                <tx:method name="merge*" propagation="REQUIRED" />
                <tx:method name="del*" propagation="REQUIRED" />
                <tx:method name="remove*" propagation="REQUIRED" />
                <tx:method name="put*" propagation="REQUIRED" />
                <tx:method name="use*" propagation="REQUIRED"/>
                <!--hibernate3必须配置为开启事务 否则 getCurrentSession()获取不到-->
                <tx:method name="get*" propagation="REQUIRED" read-only="true" />
                <tx:method name="count*" propagation="REQUIRED" read-only="true" />
                <tx:method name="find*" propagation="REQUIRED" read-only="true" />
                <tx:method name="list*" propagation="REQUIRED" read-only="true" />
                <tx:method name="*" read-only="true" />
            </tx:attributes>
        </tx:advice>
        <aop:config expose-proxy="true">
            <!-- 只对业务逻辑层实施事务 -->
            <aop:pointcut id="txPointcut" expression="execution(* com.test.*.service..*.*(..))" />
            <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
        </aop:config>
    你参照这修改把,