【问题标题】:How to Solve : org.hibernate.HibernateException: createCriteria is not valid without active transaction如何解决:org.hibernate.HibernateException: createCriteria is not valid without active transaction
【发布时间】:2015-05-25 11:21:36
【问题描述】:

我正在学习 Spring 4.0 + Hibernate 4.3 的集成,我对这两种技术都很陌生。在解决之前的问题后,我遇到了错误。请任何人指导我成功完成此操作。

applicationContext.xml

<?xml version='1.0' encoding='UTF-8' ?>
<!-- was: <?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:p="http://www.springframework.org/schema/p"
       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.0.xsd
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context-4.0.xsd
       http://www.springframework.org/schema/mvc 
       http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

    <!--bean id="propertyConfigurer"
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
          p:location="/WEB-INF/jdbc.properties" />

<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource"
    p:driverClassName="${jdbc.driverClassName}"
    p:url="${jdbc.url}"
    p:username="${jdbc.username}"
    p:password="${jdbc.password}" /-->

    <!-- ADD PERSISTENCE SUPPORT HERE (jpa, hibernate, etc) -->

</beans>

dispatcher-servlet.xml

<?xml version='1.0' encoding='UTF-8' ?>
<!-- was: <?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:p="http://www.springframework.org/schema/p"
       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.0.xsd
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context-4.0.xsd
       http://www.springframework.org/schema/mvc 
       http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

    <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
    <context:component-scan base-package="com.controller"/>
    <mvc:annotation-driven />


    <!--
    Most controllers will use the ControllerClassNameHandlerMapping above, but
    for the index controller we are using ParameterizableViewController, so we must
    define an explicit mapping for it.
    -->
    <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="index.htm">indexController</prop>
            </props>
        </property>
    </bean>

    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/WEB-INF/jsp/"
          p:suffix=".jsp" />

    <!--
    The index controller.
    -->
    <bean name="indexController"
          class="org.springframework.web.servlet.mvc.ParameterizableViewController"
          p:viewName="index" />

</beans>

hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
    <property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property>
    <property name="hibernate.connection.username">scott</property>
    <property name="hibernate.connection.password">tiger</property>
    <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
    <property name="hibernate.current_session_context_class">thread</property>
    <mapping resource="com/entity/EmpTest.hbm.xml"/>
  </session-factory>
</hibernate-configuration>

EmpModelImpl.java

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.model;

import com.entity.EmpTest;
import com.util.HibernateUtil;
import java.math.BigDecimal;
import java.util.List;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;


@Repository
public class EmpModelImp {


   private SessionFactory session;

    public void add(EmpTest emp) {
       session=HibernateUtil.getSessionFactory();
       session.getCurrentSession().save(emp);// throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }


    public void updateEmp(EmpTest emp) {
        session=HibernateUtil.getSessionFactory();
       session.getCurrentSession().update(emp);//  throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }


    public void delete(BigDecimal EmpId) {
     session=HibernateUtil.getSessionFactory();
        session.getCurrentSession().delete(getEmp(EmpId));// throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }


    public EmpTest getEmp(BigDecimal EmpId) {
        session=HibernateUtil.getSessionFactory();
        return (EmpTest) session.getCurrentSession().get(EmpTest.class, EmpId);// //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }


    public List<EmpTest> getAll() {
        session=HibernateUtil.getSessionFactory();
       return session.getCurrentSession().createCriteria("from emptest").list(); //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

}

EmpServiceImpl.java

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.model;

import com.entity.EmpTest;
import java.math.BigDecimal;
import java.util.List;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class EmpServiceImpl {
 private EmpModelImp dao;   

    public EmpServiceImpl() {
        this.dao = new EmpModelImp();
    }
    @Transactional
    public void add(EmpTest emp) {
        dao.add(emp);
    }

    @Transactional
    public void updateEmp(EmpTest emp) {
        dao.updateEmp(emp);
    }

    @Transactional
    public void delete(BigDecimal EmpId) {
        dao.delete(EmpId);
    }

    @Transactional
    public EmpTest getEmp(BigDecimal EmpId) {
        return dao.getEmp(EmpId);
    }

    @Transactional
    public List<EmpTest> getAll() {
        return dao.getAll();
    }
}

我遇到了以下异常。

org.hibernate.HibernateException: createCriteria is not valid without active transaction
    org.hibernate.context.internal.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:352)
    com.sun.proxy.$Proxy98.createCriteria(Unknown Source)
    com.model.EmpModelImp.getAll(EmpModelImp.java:51)
    com.model.EmpServiceImpl.getAll(EmpServiceImpl.java:47)

【问题讨论】:

    标签: java spring hibernate netbeans createcriteria


    【解决方案1】:

    您还需要开始和结束交易。

            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            session.beginTransaction();
           EmpTest empTest = (EmpTest) session.get(EmpTest.class, EmpId);
            session.getTransaction().commit();
    

    你当然可以让spring帮你做事务管理。但为此,您需要配置额外的事务管理器 bean。

    【讨论】:

    • 在这里查看更多关于在春季使用事务管理器的信息。 springbyexample.org/examples/…
    • 这两种方法有什么区别?
    • 而不是session.getCurrentSession().get(EmpTest.class, EmpId); 我写了session.get(EmpTest.class, EmpId); 并且它有效!我已经编辑了相同的答案。
    【解决方案2】:

    您可以通过调用getCurrentSession() 获得您的Session。这给出了绑定到事务生命周期的“当前会话”,并将在事务结束(提交或回滚)时自动刷新和关闭。因此,在这种情况下,您必须明确地开始 transaction。不要忘记commit 否则您的会话将保持打开状态并且连接不会被释放。

    或者,如果您决定使用sessionFactory.openSession(),您必须自己管理会话并“手动”刷新和关闭它。在这种情况下,您不需要为只读操作显式启动事务。

    【讨论】:

      【解决方案3】:

      将@Transactional 移动到您的模型Impl 类中,这样它将是

      @Transactional
      @Repository
      public class EmpModelImp{..}
      

      【讨论】:

        【解决方案4】:

        我认为您必须在 HQL 查询中将from EmpTest 的首字母大写

         public List<EmpTest> getAll() {
                session=HibernateUtil.getSessionFactory();
               return session.getCurrentSession().createCriteria("from Emptest").list(); 
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2022-12-11
          • 2019-12-28
          • 1970-01-01
          • 1970-01-01
          • 2012-12-07
          • 2013-01-28
          • 2023-02-20
          相关资源
          最近更新 更多