當集成Hibernate和Spring時,我遇到了問題。當我運行程序時,我會得到No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
Hibernate的Spring集成錯誤:沒有休眠會話綁定到線程
我檢查了論壇,但其中大多數與事務管理器或丟失的上下文有關:組件掃描。但我擁有它。
package com.cmpt.project.persistence;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
@Repository
public class WarehouseDao {
private SessionFactory sessionFactory;
@Autowired
public WarehouseDao (SessionFactory sessionFactory){
this.sessionFactory = sessionFactory;
}
public Session currentSession() {
return sessionFactory.getCurrentSession();
}
}
,這是我的配置文件彈簧hibernate.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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<context:annotation-config />
<context:component-scan base-package="com.cmpt.project.persistence"/>
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/spring_demo"></property>
<property name="username" value="username"></property>
<property name="password" value="password"></property>
<property name="initialSize" value="5"></property>
<property name="maxActive" value="10"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="packagesToScan"
value="com.cmpt.project.model">
</property>
<property name="hibernateProperties">
<props>
<prop key="dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
</props>
</property>
</bean>
<!-- <bean
class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor">
</bean> -->
</beans>
這裏是我的主要
package com.cmpt.project.app;
import org.hibernate.Session;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.cmpt.project.model.Customer;
import com.cmpt.project.persistence.WarehouseDao;
public class ProjectDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-hibernate.xml");
WarehouseDao dao = ctx.getBean("warehouseDao", WarehouseDao.class);
Session session = dao.currentSession();
}
}
如上圖所示,我甚至沒有更新數據庫。我甚至無法獲得會話對象。請幫忙,謝謝。
你必須添加一個事務管理器並做適當的事務設置。 Spring使用這些信息,所以它知道何時開始一個事務並從而獲得一個會話。 –
您需要具有裝飾Hibernate DAO實現類的'@ Transactional'註釋。 – Tiny
我在WarehouseDao類添加@Transactional,和設置<豆ID = 「transactionManager的」 類= 「org.springframework.orm.hibernate3.HibernateTransactionManager」> <屬性名= 「SessionFactory的」 REF = 「的sessionFactory」> 但同樣的問題仍然存在 –