Spring + Hibernate的不自动打开会话(Spring + Hibernate don&

2019-08-17 09:53发布

我使用下面的架构在Spring应用程序开发的Hibernate的DAO:

我在我宣布,我要的CRUD方法,然后我创建一个实现这个接口的具体类PersonDAOImpl一个的PersonDAO接口,这一个:

package org.andrea.myexample.HibernateOnSpring.dao;

import org.andrea.myexample.HibernateOnSpring.entity.Person;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository
@Transactional
public class PersonDAOImpl2 {

    // Factory per la creazione delle sessioni di Hibernate:
        private static SessionFactory sessionFactory;

        // Metodo Setter per l'iniezione della dipendenza della SessionFactory:
        public void setSessionFactory(SessionFactory sessionFactory) {
            this.sessionFactory = sessionFactory;
            System.out.println("Ho iniettato la SessionFactory: " + sessionFactory);

        }

        /** CREATE CRUD Operation:
         * Aggiunge un nuovo record rappresentato nella tabella rappresentato
         * da un oggetto Person
         */
        @Transactional(readOnly = false)
        public Integer addPerson(Person p) {

            System.out.println("Inside addPerson()");
            System.out.println("Connessione aperta: " + sessionFactory.getCurrentSession().isOpen());
            System.out.println("E' connesa:" + sessionFactory.getCurrentSession().isConnected());

            Integer personID = personID = (Integer) sessionFactory.getCurrentSession().save(p);

            return personID;

        }

}

然后,我已创建一个MainApp类conteining的main()方法来测试它是如何工作的。

问题是,当我执行MainApp当我尝试插入我得到的堆栈跟踪以下错误信息数据库的新纪录:

INFO: Using DataSource [org.apache.commons.dbcp.BasicDataSource@446adaa2] of Hibernate SessionFactory for HibernateTransactionManager
Ho iniettato la SessionFactory: org.hibernate.internal.SessionFactoryImpl@34a8a14b
Contesto recuperato: org.springframework.context.support.ClassPathXmlApplicationContext@70501e4e: startup date [Sat Mar 09 10:02:37 CET 2013]; root of context hierarchy
Creato persona1: org.andrea.myexample.HibernateOnSpring.entity.Person@2d35bcd7
Exception in thread "main" java.lang.ClassCastException: org.andrea.myexample.HibernateOnSpring.dao.PersonDAOImpl2$$EnhancerByCGLIB$$d0c4a932 cannot be cast to org.andrea.myexample.HibernateOnSpring.dao.PersonDAO
    at org.andrea.myexample.HibernateOnSpring.MainApp.main(MainApp.java:26)

好像它没有找到Session对象,现在我有一个疑问:我知道,春天开放,并自动会话关闭每一个@Transactional方法,但它似乎不工作!

相反,如果我明确地打开一个新的会话时,我调用该方法,它工作得很好......这样:

@Transactional(readOnly = false)
    public Integer addPerson(Person p) {

        System.out.println("Inside addPerson()");

        Session session = sessionFactory.openSession();

        Transaction tx = null;
        Integer personID = null;

        try {
            tx = session.beginTransaction();

            personID = (Integer) session.save(p);
            tx.commit();
        } catch (HibernateException e) {
            if (tx != null)
                tx.rollback();
            e.printStackTrace();
        } finally {
            session.close();
        }

        return personID;

    }

但我认为,我可以说春天来自动打开Session时,在输入方法,并关闭它,当退出

这是我的XML配置文件:

<?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:tx="http://www.springframework.org/schema/tx" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd        
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-3.1.xsd ">

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/SpringTestDb" />
        <property name="username" value="root" />
        <property name="password" value="MY_DB_PSWD" />
        <property name="initialSize" value="2" />
        <property name="maxActive" value="5" />
    </bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan" value="org.andrea.myexample.HibernateOnSpring.entity" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.show_sql">false</prop>
            </props>
        </property>
    </bean>

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

    <tx:annotation-driven transaction-manager="transactionManager"/>

    <bean id="personDAOImpl" class="org.andrea.myexample.HibernateOnSpring.dao.PersonDAOImpl2" >
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <!-- Register @Autowired annotation -->
    <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
</beans>

有人能帮助我吗?

TNX

安德里亚

Answer 1:

你需要或者使用的接口为您的DAO或重新配置代理机制使用类。



Answer 2:

你必须告诉春时“打开”会议。 没有准备好这proccesing请求之前打开的会话使用Spring实现。

<mvc:interceptors>
    <bean name="openSessionInViewInterceptor" class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor">
        <property name="sessionFactory">
            <ref local="sessionFactory"/>
        </property>
    </bean>
</mvc:interceptors>


文章来源: Spring + Hibernate don't open automatically the session