125

我用 spring + hibernate 创建了一个应用程序,但我总是得到这个错误。这是我第一个使用休眠的应用程序,我阅读了一些指南,但我无法解决这个问题。我在哪里做错了?

这是我的应用程序的代码

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)

学生.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

学生道.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

主应用程序.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)
);
4

19 回答 19

229

您必须启用事务支持(<tx:annotation-driven>@EnableTransactionManagement)并声明transactionManager应该通过SessionFactory.

您必须添加@Transactional到您的@Repository

@Transactional你的@RepositorySpring 中,能够将事务支持应用到你的存储库中。

您的Student类没有@javax.persistence.*注释@Entity,我假设该类的映射配置已通过 XML 定义。

于 2014-10-05T14:43:35.047 回答
40

我遇到了同样的问题,但是在一个不属于服务层的类中。在我的例子中,事务管理器只是通过getBean()方法从上下文中获取的,并且该类属于视图层——我的项目使用了OpenSessionInView技术。

sessionFactory.getCurrentSession()方法一直导致与作者相同的异常。对我来说解决方案相当简单。

Session session;

try {
    session = sessionFactory.getCurrentSession();
} catch (HibernateException e) {
    session = sessionFactory.openSession();
}

如果该getCurrentSession()方法失败,则openSession()应该可以解决问题。

于 2016-04-25T12:04:18.547 回答
14

在类服务中添加spring的注解@Transactional

于 2015-12-16T09:49:18.520 回答
3

在你的 xyz.DAOImpl.java

执行以下步骤:

//Step-1: 设置会话工厂

@Resource(name="sessionFactory")
private SessionFactory sessionFactory;

public void setSessionFactory(SessionFactory sf)
{
    this.sessionFactory = sf;
}

//Step-2:尝试获取当前会话,并捕获HibernateException异常。


//Step-3:如果有任何HibernateException异常,则true获取openSession。

try 
{
    //Step-2: Implementation
    session = sessionFactory.getCurrentSession();
} 
catch (HibernateException e) 
{
    //Step-3: Implementation
    session = sessionFactory.openSession();
}
于 2016-05-20T13:59:48.520 回答
3

您需要允许对您的 DAO 方法进行交易。添加,

@Transactional(readOnly = true, propagation=Propagation.NOT_SUPPORTED)

在你的道法之上。并且@Transactional应该来自包装:

org.springframework.transaction.annotation.Transactional
于 2018-10-04T13:44:18.810 回答
2

我在 web.xml 中添加了这些配置,对我来说效果很好!

<filter>
    <filter-name>OpenSessionInViewFilter</filter-name>
    <filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
    <init-param>
        <param-name>sessionFactoryBeanName</param-name>
        <param-value>sessionFactory</param-value>
    </init-param>
    <init-param>
        <param-name>flushMode</param-name>
        <param-value>AUTO</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>OpenSessionInViewFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

此外,排名最高的答案为我提供了防止应用程序在第一次运行时出现恐慌的线索。

于 2017-10-25T03:46:46.160 回答
1

我的解决方案是(使用 Spring)将失败的方法放在另一个创建和提交事务的方法中。

为此,我首先注入了以下内容:

@Autowired
private PlatformTransactionManager transactionManager;

最后做到了:

public void newMethod() {
    DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
    TransactionStatus transaction = transactionManager.getTransaction(definition);

    oldMethod();

    transactionManager.commit(transaction);
}
于 2019-01-17T14:27:49.327 回答
1

@Transactional =javax.transaction.Transactional. 放在旁边@Repository

于 2015-12-28T08:12:30.327 回答
1

我也有这个错误,因为在我使用@Transactional 注释的文件中,我导入了错误的类

import javax.transaction.Transactional; 

而不是 javax,使用

import org.springframework.transaction.annotation.Transactional; 
于 2017-12-23T21:59:54.017 回答
0

我的数据库表的列名与 Java 对象 (@Entity) 不匹配,这会导致引发上述异常。

通过使用适当的列名更新表可以解决此问题。

于 2020-12-08T10:26:13.373 回答
0

我的配置是这样的。我有一个QuartzJob、一个 Service Bean 和 Dao。像往常一样,它配置了 LocalSessionFactoryBean(用于休眠)和用于 Quartz 框架的 SchedulerFactoryBean。在编写 Quartz 作业时,我错误地使用 @Service 对其进行了注释我不应该这样做,因为我正在使用另一种策略来连接QuartzBean,使用AutowiringSpringBeanJobFactory扩展SpringBeanJobFactory

所以实际发生的事情是,由于 Quartz Autowire,TX 被注入到 Job Bean 中,同时 Tx 上下文是通过 @Service 注释设置的因此 TX 不同步!

我希望它对那些上述解决方案确实没有解决问题的人有所帮助。我使用的是 Spring 4.2.5 和 Hibernate 4.0.1 ,

我看到在这个线程中有一个不必要的建议将 @Transactional 注释添加DAO( @Repository ) ,这是一个无用的建议,因为@Repository拥有它需要的所有东西,不必专门设置@transactional on DAOs,因为 DAOs 是从@Trasancational已经注入的服务中调用的。我希望这可能对一起使用 Quartz 、 Spring 和 Hibernate 的人有所帮助。

于 2018-02-13T03:04:02.790 回答
0

我遇到了同样的问题,最后发现<tx:annotaion-driven />没有在[dispatcher]-servlet.xmlwhere component-scan element enable @serviceannotated 类中定义。

简单地将<tx:annotaion-driven />组件扫描元素放在一起,问题就消失了。

于 2017-04-20T01:41:34.013 回答
0

我遇到过同样的问题。我通过以下方式解决了它:

  1. 将此行添加到dispatcher-servlet文件中:

    <tx:annotation-driven/>
    

    检查<beans>同一文件中的上述部分。这两行必须存在:

    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation= "http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"
    
  2. @Repository还要确保您添加@TransactionalsessionFactory.

    @Repository
    @Transactional
    public class ItemDaoImpl implements ItemDao {
        @Autowired
        private SessionFactory sessionFactory;
    
于 2020-06-12T20:28:27.727 回答
0

检查你的 dao 类。它必须是这样的:

Session session = getCurrentSession();
Query query = session.createQuery(GET_ALL);

和注释:

@Transactional
@Repository
于 2018-06-06T18:42:11.593 回答
0

我的类似问题通过以下两种方法得到解决。

1)通过人工处理交易:

Session session = sessionFactory.getCurrentSession();
Transaction tx = session.beginTransaction();
UserInfo user = (UserInfo) session.get(UserInfo.class, 1);
tx.commit();

2)告诉Spring在您的过滤器中为您打开和管理事务web.xml并确保使用@Repository @Transactional

<filter>
  <filter-name>hibernateFilter</filter-name>
  <filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
  <init-param>
    <param-name>sessionFactory</param-name>
    <param-value>session.factory</param-value>
  </init-param>
</filter>
<filter-mapping>
  <filter-name>hibernateFilter</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>
于 2017-08-14T06:02:27.767 回答
0

在上面的这个类中,@Repository只放置了一个注释@Transactional,它将起作用。如果它有效回复(Y/ N):

@Repository
@Transactional
public class StudentDAOImpl implements StudentDAO
于 2019-09-30T06:16:09.397 回答
0

transaction-managerspring-servlet.xml<annotation-driven/>中添加:

<tx:annotation-driven transaction-manager="yourTransactionBeanID"/>
于 2018-06-01T18:48:03.767 回答
0

感谢 mannedear 的评论。我使用 springmvc,在我的情况下,我必须使用 as

@Repository
@Transactional
@EnableTransactionManagement
public class UserDao {
...
}

我还将 spring-context 添加到 pom.xml 并且它可以工作

于 2020-05-16T06:02:34.403 回答
0

就我而言,问题是控制器试图使用@Repository 直接访问DAO。在@Repository 之上添加@Service 层解决了这个问题

于 2021-11-15T18:06:56.213 回答