2016-04-14 55 views
0

彈簧數據庫請求處理失敗;嵌套異常是org.hibernate.HibernateException:對當前線程

<?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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" 
     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/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
          http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> 

    <context:component-scan base-package="com.hello.hellospring" /> 
    <context:property-placeholder location="classpath:application.properties" /> 
    <tx:annotation-driven transaction-manager="transactionManager" /> 


    <bean id="dataSource" 
      class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
     <property name="driverClassName" value="${jdbc.driverClassName}" /> 
     <property name="url" value="${jdbc.url}" /> 
     <property name="username" value="${jdbc.username}" /> 
     <property name="password" value="${jdbc.password}" /> 
    </bean> 

    <bean id="sessionFactory" 
      class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
     <property name="dataSource" ref="dataSource" /> 
     <property name="packagesToScan"> 
      <list> 
       <value>com.hello.hellospring.model</value> 
      </list> 
     </property> 
     <property name="hibernateProperties"> 
      <props> 
       <prop key="hibernate.dialect">${hibernate.dialect}</prop> 
       <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> 
       <prop key="hibernate.format_sql">${hibernate.format_sql}</prop> 
      </props> 
     </property> 
    </bean> 

    <bean id="transactionManager" 
      class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 
     <property name="sessionFactory" ref="sessionFactory" /> 
    </bean> 

    <bean id="persistenceExceptionTranslationPostProcessor" 
      class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" /> 

</beans> 

web.xml中無法獲得交易同步會話

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

    <display-name>Spring Security Bcrypt Application</display-name> 

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

    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value> 
      /WEB-INF/applicationContext.xml 
      /WEB-INF/spring-database.xml 
      /WEB-INF/spring-security.xml 
     </param-value> 
    </context-param> 

    <!-- Spring MVC --> 
    <servlet> 
     <servlet-name>springmvc-dispatcher</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <init-param> 
      <param-name>contextConfigLocation</param-name> 
      <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value> 
     </init-param> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>springmvc-dispatcher</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 


    <!-- Spring Security Filter --> 
    <filter> 
     <filter-name>springSecurityFilterChain</filter-name> 
     <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> 
    </filter> 

    <filter-mapping> 
     <filter-name>springSecurityFilterChain</filter-name> 
     <url-pattern>/*</url-pattern> 
    </filter-mapping> 

</web-app> 

userDaoImpl.java

package com.hello.hellospring.dao; 

import org.hibernate.Criteria; 
import org.hibernate.criterion.Restrictions; 
import org.springframework.stereotype.Repository; 

import com.hello.hellospring.model.User; 


@Repository("userDao") 
public class UserDaoImpl extends AbstractDao<Integer, User> implements UserDao { 

    public void save(User user) { 
     persist(user); 
    } 

    public User findById(int id) { 
     return getByKey(id); 
    } 

    public User findBySSO(String sso) { 
     Criteria crit = createEntityCriteria(); 
     crit.add(Restrictions.eq("ssoId", sso)); 
     return (User) crit.uniqueResult(); 
    } 

} 

這裏是我得到的錯誤類:

AbstractDao.java

package com.hello.hellospring.dao; 

import java.io.Serializable; 

import java.lang.reflect.ParameterizedType; 

import org.hibernate.Criteria; 
import org.hibernate.Session; 
import org.hibernate.SessionFactory; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.transaction.annotation.Transactional; 



public abstract class AbstractDao<PK extends Serializable, T> { 

    private final Class<T> persistentClass; 

    @SuppressWarnings("unchecked") 
    public AbstractDao(){ 
     this.persistentClass =(Class<T>) ((ParameterizedType) this.getClass().getGenericSuperclass()).getActualTypeArguments()[1]; 
    } 

    @Autowired 
    private SessionFactory sessionFactory; 

    @Transactional 
    protected Session getSession(){ 
     return sessionFactory.getCurrentSession(); 
    } 


    @SuppressWarnings("unchecked") 
    public T getByKey(PK key) { 
     return (T) getSession().get(persistentClass, key); 
    } 


    public void persist(T entity) { 
     getSession().persist(entity); 
    } 


    public void delete(T entity) { 
     getSession().delete(entity); 
    } 


    protected Criteria createEntityCriteria(){ 
     return getSession().createCriteria(persistentClass); 
    } 


} 

我不知道什麼是錯,我得到的問題是:請幫我解決這個問題。每次我運行這段代碼時,都會說我有一些休眠會話問題。謝謝:)

enter image description here enter image description here

+0

嗨我有一些問題與通用道這是什麼解決方案? –

回答

0

嘗試註釋你的類或方法使用@Transactional。聲明式事務邊界將取消此例外。

+0

你應該提供與CODE一起的詳細解釋的答案。如果您不確定,請發表評論,而不是發帖作爲回答。 – AADProgramming

0
package com.hello.hellospring.dao; 

    import org.hibernate.Criteria; 
    import org.hibernate.criterion.Restrictions; 
    import org.springframework.stereotype.Repository; 

    import com.hello.hellospring.model.User; 


    @Repository("userDao") 
    @Transactional 
    public class UserDaoImpl extends AbstractDao<Integer, User> implements UserDao { 

     public void save(User user) { 
      persist(user); 
     } 

     public User findById(int id) { 
      return getByKey(id); 
     } 

     public User findBySSO(String sso) { 
      Criteria crit = createEntityCriteria(); 
      crit.add(Restrictions.eq("ssoId", sso)); 
      return (User) crit.uniqueResult(); 
     } 

    } 

You are missing a @Transactional annotation above this class read this http://www.springbyexample.org/examples/hibernate-transaction-annotation-config-code-example.html 
+0

我已經嘗試了這一點,但仍面臨同樣的問題。我已經在每個服務類中應用@Transactionl,而不是 –

+0

它在此行中顯示錯誤:protected Session getSession(){ return sessionFactory.getCurrentSession(); } –

+0

不要把事務無處不在,就在上面的UserDaoImpl類 –

相關問題