Spring Hibernate - 无法获取当前线程的事务同步会话 [英] Spring Hibernate - Could not obtain transaction-synchronized Session for current thread
问题描述
我用spring + hibernate创建了一个应用程序,但是我总是得到这个错误。这是我第一次使用hibernate的应用程序,我阅读了一些指南,但是我无法解决这个问题。
这是我的应用程序的代码
ott 05,2014 4:03:06 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息:刷新org.springframework.context.support.ClassPathXmlApplicationContext@1eab16b:启动日期[Sun Oct 05 16:03 :06 CEST 2014];上下文层次结构的根
ott 05,2014 4:03:06 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
Informazioni:从类路径资源加载XML bean定义[springConfig.xml]
ott 05,2014 4:03:08 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager< clinit>
INFO:HCANN000001:Hibernate Commons Annotations {4.0.5.Final}
ott 05,2014 4:03:08 PM org.hibernate.Version logVersion
INFO:HHH000412:Hibernate Core {4.3 .6.Final}
ott 05,2014 4:03:08 PM org.hibernate.cfg.Environment< clinit>
INFO:HHH000206:找不到hibernate.properties
ott 05,2014 4:03:08 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO:HHH000021:字节码提供程序名称:javassist
ott 05,2014 4:03:09 PM org.hibernate.dialect.Dialect< init>
INFO:HHH000400:使用方言:org.hibernate.dialect.MySQL5Dialect
ott 05,2014 4:03:09 PM org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
INFO: HHH000399:使用默认事务策略(直接JDBC事务)
ott 05,2014 4:03:09 PM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory< init>
INFO:HHH000397:在线程main中使用ASTQueryTranslatorFactory
异常org.hibernate.HibernateException:无法获取当前线程的事务同步会话
在org.springframework.orm.hibernate4.SpringSessionContext .currentSession(SpringSessionContext.java:134)
at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1014)
at coreservlets.StudentDAOImpl.create(StudentDAOImpl.java:19)
在coreservlets.MainApp.main(MainApp.java:14)
student.java
package coreservlets;
public class Student {
private Integer id;
私人字符串名称;
私人整数年龄;
public Integer getId(){return id;} // getId
public void setId(Integer id){this.id = id;} // setId
public String getName(){return name;} // getName
$ b $ public void setName(String name){this.name = name;} // setName
public Integer getAge(){return age;} // getAge
$ b $ public void setAge(Integer age){this.age = age;} // setAge
} // Student
studentDAO.java
package coreservlets;
import org.hibernate.SessionFactory;
public interface StudentDAO {
public void setSessionFactory(SessionFactory sessionFactory);
public void create(String name,Integer age);
$ b $ // StudentDAO
StudentDAOImpl.java
package coreservlets;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
@Repository
public class StudentDAOImpl实现StudentDAO {
private SessionFactory sessionFactory;
@Autowired
public void setSessionFactory(SessionFactory sessionFactory){
this.sessionFactory = sessionFactory;
} // setSessionFactory
public void create(String name,Integer age){
Session session = sessionFactory.getCurrentSession();
学生=新生();
student.setName(name);
student.setAge(age);
session.save(student);
} //创建
} // StudentDAOImpl
MainApp .java
package coreservlets;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
public static void main(String [] args){
ApplicationContext context = new ClassPathXmlApplicationContext(springConfig.xml );
StudentDAOImpl student =(StudentDAOImpl)context.getBean(studentDAOImpl);
student.create(Alessandro,新Integer(33));
$ b} //主要
} // MainApp
springConfig.xml
<?xml version =1.0encoding =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
xmlns:mvc =http://www.springframework.org/schema/mvc
xsi :schemaLocation =http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework。 org / schema / context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework .ORG /模式/ MVC /弹簧-MVC-4.1.xsd>
< context:annotation-config />
< context:component-scan base-package =coreservlets/>
< bean id =dataSourceclass =org.apache.commons.dbcp2.BasicDataSource>
< property name =driverClassNamevalue =com.mysql.jdbc.Driver/>
< property name =urlvalue =jdbc:mysql:// localhost:3306 / spring_hibernate/>
< property name =usernamevalue =root/>
< property name =passwordvalue =password/>
< property name =initialSizevalue =5/>
< property name =maxTotalvalue =10/>
< / bean>
< bean id =sessionFactoryclass =org.springframework.orm.hibernate4.LocalSessionFactoryBean>
< property name =dataSourceref =dataSource/>
< property name =hibernateProperties>
<值>
hibernate.dialect = org.hibernate.dialect.MySQLDialect
< / value>
< / property>
< / bean>
< / beans>
create table student
(
id整数不为空auto_increment,
name varchar(20)非空,
年龄整数非空,
主键id)
);
您必须启用事务支持(< tx:注解驱动>
或 @EnableTransactionManagement
)和声明 transactionManager
,它应该通过 SessionFactory
。
您必须将 @Transactional
添加到 @Repository
在 @Repository
中使用 @Transactional
Spring可以将事务支持应用到您的存储库中。
您的 Student
类没有@ javax.persistence。*注释 @Entity
,我假设该类的映射配置已通过XML定义。
I created an application with spring + hibernate, but I always get this error. This is my first application with hibernate, I read some guides but I can not solve this problem. Where am I doing wrong?
This is the code of my application
ott 05, 2014 4:03:06 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
Informazioni: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1eab16b: startup date [Sun Oct 05 16:03:06 CEST 2014]; root of context hierarchy
ott 05, 2014 4:03:06 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
Informazioni: Loading XML bean definitions from class path resource [springConfig.xml]
ott 05, 2014 4:03:08 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.5.Final}
ott 05, 2014 4:03:08 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.3.6.Final}
ott 05, 2014 4:03:08 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
ott 05, 2014 4:03:08 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
ott 05, 2014 4:03:09 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
ott 05, 2014 4:03:09 PM org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
INFO: HHH000399: Using default transaction strategy (direct JDBC transactions)
ott 05, 2014 4:03:09 PM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory
Exception in thread "main" org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread
at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:134)
at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1014)
at coreservlets.StudentDAOImpl.create(StudentDAOImpl.java:19)
at coreservlets.MainApp.main(MainApp.java:14)
student.java
package coreservlets;
public class Student {
private Integer id;
private String name;
private Integer age;
public Integer getId(){return id;}//getId
public void setId(Integer id){this.id=id;}//setId
public String getName(){return name;}//getName
public void setName(String name){this.name=name;}//setName
public Integer getAge(){return age;}//getAge
public void setAge(Integer age){this.age=age;}//setAge
}//Student
studentDAO.java
package coreservlets;
import org.hibernate.SessionFactory;
public interface StudentDAO {
public void setSessionFactory(SessionFactory sessionFactory);
public void create(String name,Integer age);
}//StudentDAO
StudentDAOImpl.java
package coreservlets;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
@Repository
public class StudentDAOImpl implements StudentDAO {
private SessionFactory sessionFactory;
@Autowired
public void setSessionFactory(SessionFactory sessionFactory){
this.sessionFactory=sessionFactory;
}//setSessionFactory
public void create(String name,Integer age){
Session session=sessionFactory.getCurrentSession();
Student student=new Student();
student.setName(name);
student.setAge(age);
session.save(student);
}//create
}//StudentDAOImpl
MainApp.java
package coreservlets;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("springConfig.xml");
StudentDAOImpl student=(StudentDAOImpl) context.getBean("studentDAOImpl");
student.create("Alessandro", new Integer(33));
}//main
}//MainApp
springConfig.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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
<context:annotation-config/>
<context:component-scan base-package="coreservlets"/>
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/spring_hibernate"/>
<property name="username" value="root"/>
<property name="password" value="password"/>
<property name="initialSize" value="5"/>
<property name="maxTotal" value="10"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQLDialect
</value>
</property>
</bean>
</beans>
sql
create table student
(
id integer not null auto_increment,
name varchar(20) not null,
age integer not null,
primary key(id)
);
You must enable the transaction support (<tx:annotation-driven>
or @EnableTransactionManagement
) and declare the transactionManager
and it should work through the SessionFactory
.
You must add @Transactional
into your @Repository
With @Transactional
in your @Repository
Spring is able to apply transactional support into your repository.
Your Student
class has no the @javax.persistence.* annotations how @Entity
, I am assuming the Mapping Configuration for that class has been defined through XML.
这篇关于Spring Hibernate - 无法获取当前线程的事务同步会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!