本文介绍了org.hibernate.HibernateException:如果没有活动事务,createSQLQuery无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我想通过休眠连接到我的数据库时,我收到了这个异常,我尝试了很多我在互联网上发现的东西,但没有任何帮助,我的一些文件:
dao类连接:

  @Repository 
public class UserDaoImpl implements UserDao {

@Autowired
SessionFactory sessionFactory;
//查询的问题在这里
public List< User> getAllUsers(){
return sessionFactory.getCurrentSession()。createSQLQuery(SELECT * FROM user)。list();
}

}

web.xml:

 <?xml version =1.0encoding =UTF-8?> 
< web-app version =2.5xmlns =http://java.sun.com/xml/ns/javaee
xmlns:xsi =http://www.w3。 org / 2001 / XMLSchema-instance
xsi:schemaLocation =http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web- app_2_5.xsd>


< display-name>原型创建的Web应用程序< / display-name>

< servlet>
< servlet-name>调度程序< / servlet-name>
< servlet-class> org.springframework.web.servlet.DispatcherServlet< / servlet-class>
1< / load-on-startup>
< / servlet>

< servlet-mapping>
< servlet-name>调度程序< / servlet-name>
< url-pattern> *。htm< / url-pattern>
< / servlet-mapping>

< context-param>
< param-name> contextConfigLocation< / param-name>
< param-value> classpath:context.xml< / param-value>
< / context-param>

< listener>
< listener-class> org.springframework.web.context.ContextLoaderListener< / listener-class>
< / listener>

< / web-app>

我的servlet:

 <?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-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework。 org / schema / mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd\">

< context:annotation-config />
< context:component-scan base-package =com.lime/>

< mvc:annotation-driven />
< mvc:default-servlet-handler />

< bean id =viewResolverclass =org.springframework.web.servlet.view.InternalResourceViewResolver>
< property name =viewClassvalue =org.springframework.web.servlet.view.JstlView/>
< property name =prefixvalue =/ WEB-INF //>
< property name =suffixvalue =。jsp/>
< / bean>

< / beans>

和context.xml:

 <?xml version =1.0encoding =UTF-8?> 
< beans xmlns =http://www.springframework.org/schema/beans
xmlns:xsi =http://www.w3.org/2001/XMLSchema-instance
xsi:schemaLocation =http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd\">

< bean id =sessionFactoryclass =org.springframework.orm.hibernate4.LocalSessionFactoryBean>
< property name =hibernateProperties>
<道具>
< prop key =hibernate.dialect> org.hibernate.dialect.PostgreSQL9Dialect< / prop>
< prop key =hibernate.show_sql> true< / prop>
< prop key =hibernate.current_session_context_class> thread< / prop>
< prop key =hibernate.connection.driver_class> org.postgresql.Driver< / prop>
< prop key =hibernate.connection.url> jdbc:postgresql:// localhost:5432 / come_to_blog_db< / prop>
< prop key =hibernate.connection.username> postgres< / prop>
< prop key =hibernate.connection.password> admin< / prop>
< /道具>
< / property>
< property name =annotatedClasses>
< list>
< value> com.lime.model.User< / value>
< / list>
< / property>
< / bean>

< / beans>


解决方案

b

  @Repository 
public class UserDaoImpl implements UserDao {
$ b $ @Autowired
SessionFactory sessionFactory;
//查询的问题在这里
public List< User> getAllUsers(){
Session session = null;
尝试
{
Session session = sessionFactory.openSession();
return session.createSQLQuery(SELECT * FROM user)。list();

catch(例外e)
{
//记录
}
终于
{
if(session!= null&& session.isOpen)
{
session.close();
session = null;
}
}
}

}



使用genericDAO获取当前会话需要使用openSession()明确打开,而getCurrentSession()只需要附加一个使用openSession()的 p 更新它到当前会话。根据作者


I'm getting this exception when i want to connect to my database via hibernate, i was trying a lot of things i found on the internet but nothing helped, some of my files:dao class with connection:

@Repository
public class UserDaoImpl implements UserDao {

    @Autowired
    SessionFactory sessionFactory;
//the problem with query is here
    public List<User> getAllUsers() {
        return sessionFactory.getCurrentSession().createSQLQuery("SELECT * FROM user").list();
    }

}

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">


    <display-name>Archetype Created Web Application</display-name>

    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:context.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

</web-app>

my servlet:

<?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-3.1.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.1.xsd
           http://www.springframework.org/schema/mvc
           http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">

    <context:annotation-config />
    <context:component-scan base-package="com.lime" />

    <mvc:annotation-driven />
    <mvc:default-servlet-handler />

    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/" />
        <property name="suffix" value=".jsp" />
    </bean>

</beans>

and context.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQL9Dialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.current_session_context_class">thread</prop>
                <prop key="hibernate.connection.driver_class">org.postgresql.Driver</prop>
                <prop key="hibernate.connection.url">jdbc:postgresql://localhost:5432/come_to_blog_db</prop>
                <prop key="hibernate.connection.username">postgres</prop>
                <prop key="hibernate.connection.password">admin</prop>
            </props>
        </property>
        <property name="annotatedClasses">
            <list>
                <value>com.lime.model.User</value>
            </list>
        </property>
    </bean>

</beans>
解决方案

Just try with this

@Repository
public class UserDaoImpl implements UserDao {

@Autowired
SessionFactory sessionFactory;
//the problem with query is here
public List<User> getAllUsers() {
    Session session=null;
    try 
    {
    Session session = sessionFactory.openSession();
    return session.createSQLQuery("SELECT * FROM user").list();
    }
    catch(Exception e)
    {
     //Logging
    }
    finally
    {
        if(session !=null && session.isOpen)
        {
          session.close();
          session=null;
        }
    }
}

}

Update

with genericDAO it gets the current session which needs to be explicitly open using openSession(), while getCurrentSession() just attaches it to the current session. According to the author

这篇关于org.hibernate.HibernateException:如果没有活动事务,createSQLQuery无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 20:25