0

我在 Spring 应用程序中遇到了休眠问题。我认为一定有错误的配置。

休眠.cfg.xml

<hibernate-configuration>
    <session-factory>
        <property name="hibernate.bytecode.use_reflection_optimizer">false</property>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.password">password</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/cloud_app?autoReconnect=true</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">root</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.connection.release_mode">after_transaction</property>
        <property name="show_sql">true</property>

    ...

持久性上下文.xml

<bean id="dataSource" destroy-method="close"
        class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver" />
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/cloud_app" />
        <property name="user" value="root" />
        <property name="password" value="root" />
        <property name="debugUnreturnedConnectionStackTraces" value="true" />
        <property name="unreturnedConnectionTimeout" value="20" />
        <property name="minPoolSize" value="5" />
        <property name="initialPoolSize" value="10" />
        <property name="maxPoolSize" value="50" />
        <property name="maxStatements" value="50" />
        <property name="idleConnectionTestPeriod" value="120" />
        <property name="maxIdleTime" value="1200" />
    </bean>

    <!-- Hibernate -->
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation">
            <value>classpath:hibernate.cfg.xml</value>
        </property>
        <property name="configurationClass">
            <value>org.hibernate.cfg.AnnotationConfiguration</value>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
    </bean>

    <!-- Transaction manager -->
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
        <property name="dataSource" ref="dataSource" />
    </bean>

当我在我的应用程序中使用时sessionFactory.getCurrentSession(),它仍然会引发异常

org.hibernate.LazyInitializationException: could not initialize proxy - no Session

但是当我使用时sessionFactory.openSession(),它解决了问题,但又产生了另一个问题。所以我的问题是,我怎样才能达到一种状态——整个应用程序的一个会话。当我打电话时如何达到sessionFactory.getCurrentSession(),会话将存在。我很困惑,我阅读了很多线程如何解决它,但没有成功。

更新 当我得到一些对象然后我尝试改变它

Opportunity opportunity = opportunityDao.get(idOpportunity);
opportunity.setOrder(order);
opportunityDao.edit(opportunity);

get方法中的代码

public T get(Integer id) {
    T object = (T) sessionFactory.getCurrentSession().get(clazz, id);
    return object;
}

edit方法中的代码

public void edit(T object) {
    this.sessionFactory.getCurrentSession().update(object);
}

它把我扔了org.hibernate.HibernateException: Illegal attempt to associate a collection with two open sessions

4

1 回答 1

1

几件事

  1. 你的hibernate.connection.*属性是无用的,因为你DataSource在你的spring应用程序中注入了一个
  2. dialect 和 show_sql 的其他属性已经在 spring 配置中设置好了,所以总之(由于 1 和 2)可以删除hibernate.cfg.xml文件。
  3. 使用AnnotationSessionFactoryBean便利类。

留给你这个

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.bytecode.use_reflection_optimize">false</prop>
        </props>
    </property>
    <property name="packagesToScan" value="your.package.with.entities.here" />
</bean>

这至少可以清理您的配置(并将所有内容移动到一个文件中)。

您遇到的问题是由于不正确的事务配置。你有如何配置,HibernateTransactionManager但没有配置的时间/地点。我希望 a <tx:annotation-driven />or <tx:advice />with<aop:config />启用交易。

我建议阅读 Spring Reference Guide 中的transaction 章节,有关 hibernate 的更多信息请阅读hibernate 部分

于 2013-12-04T08:15:11.420 回答