Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何解决:org.hibernate.HibernateException:createCriteria在没有活动事务的情况下无效_Java_Spring_Hibernate_Netbeans_Createcriteria - Fatal编程技术网

Java 如何解决:org.hibernate.HibernateException:createCriteria在没有活动事务的情况下无效

Java 如何解决:org.hibernate.HibernateException:createCriteria在没有活动事务的情况下无效,java,spring,hibernate,netbeans,createcriteria,Java,Spring,Hibernate,Netbeans,Createcriteria,我正在学习Spring4.0+Hibernate4.3集成,我对这两种技术都很陌生。在解决以前的问题后,我遇到了错误。任何人请指导我成功完成这项工作 applicationContext.xml <?xml version='1.0' encoding='UTF-8' ?> <!-- was: <?xml version="1.0" encoding="UTF-8"?> --> <beans xmlns="http://www.springframewo

我正在学习Spring4.0+Hibernate4.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>
<?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>
<?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>

您还需要开始和结束事务

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

当然,您可以让spring为您进行事务管理。但为此,您需要配置额外的transactionmanager bean。

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


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

将@Transactional移动到您的model Impl类中,这样它就会

@Transactional
@Repository
public class EmpModelImp{..}

我认为您必须在HQL查询中使用大写字母从最空的
写入

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

有关在spring中使用事务管理器的更多信息,请查看此处。这两种方法的区别是什么?而不是
session.getCurrentSession().get(EmpTest.class,EmpId)我编写了会话.get(EmpTest.class,EmpId)成功了!!我已经编辑了你的答案。
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
       EmpTest empTest = (EmpTest) session.get(EmpTest.class, EmpId);
        session.getTransaction().commit();
@Transactional
@Repository
public class EmpModelImp{..}
 public List<EmpTest> getAll() {
        session=HibernateUtil.getSessionFactory();
       return session.getCurrentSession().createCriteria("from Emptest").list(); 
    }