복붙노트

[SPRING] org.hibernate.HibernateException : createSQLQuery가 활성 트랜잭션없이 유효하지 않습니다.

SPRING

org.hibernate.HibernateException : createSQLQuery가 활성 트랜잭션없이 유효하지 않습니다.

나는이 예외가 내 데이터베이스에 최대 절전 모드를 통해 연결하려고 할 때, 나는 인터넷에서 찾은 많은 것들을 시도했지만 아무런 도움이되지 않았다. 내 파일의 일부 : 연결 DAO 클래스 :

@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();
    }

}

веб.хмл :

<?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>

내 서블릿 :

<?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>

및 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>

해결법

  1. ==============================

    1.이걸로 시도해보십시오.

    이걸로 시도해보십시오.

    @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;
            }
        }
    }
    
    }
    

    최신 정보

    genericDAO를 사용하면 openSession ()을 사용하여 명시 적으로 열려야하는 현재 세션을 가져오고 getCurrentSession ()은 현재 세션에 연결하기 만하면됩니다. 저자에 따르면

  2. from https://stackoverflow.com/questions/24468602/org-hibernate-hibernateexception-createsqlquery-is-not-valid-without-active-tra by cc-by-sa and MIT license