我在 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