2013-05-30 245 views
-2

我是spring mvchibernate的新手。爲了幫助我理解我遵循本教程http://viralpatel.net/blogs/spring3-mvc-hibernate-maven-tutorial-eclipse-example/ 我收到了這個錯誤org.hibernate.HibernateException:找不到當前線程的會話。我已經將事務管理器和@transaction包含在服務實現和dao中。請在下面找到Spring mvc + hibernate + maven

錯誤日誌中的錯誤和代碼片段:

SEVERE: Servlet.service() for servlet [appServlet] in context with path [/app] threw exception [Request processing failed; nested exception is org.hibernate.HibernateException: No Session found for current thread with root cause 
org.hibernate.HibernateException: No Session found for current thread 
    at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:97) 
    at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1039) 
    at com.org.dtrack.DAO.ContactDAOImpl.listContact(ContactDAOImpl.java:29) 
    at com.org.dtrack.service.ContactServiceImpl.listContact(ContactServiceImpl.java:28) 
    at com.org.dtrack.ContactController.listContacts(ContactController.java:27) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:601) 
    at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:219) 
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132) 
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104) 
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:746) 
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:687) 
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80) 
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925) 
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856) 
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:915) 
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:811) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:621) 
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:796) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240) 
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164) 
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462) 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164) 
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100) 
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562) 
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395) 
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250) 
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188) 
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) 
    at java.lang.Thread.run(Thread.java:722) 

的pom.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 
<modelVersion>4.0.0</modelVersion> 
<groupId>com.springproject</groupId> 
<artifactId>app</artifactId> 
<name>springapp</name> 
<packaging>war</packaging> 
<version>1.0.0-BUILD-SNAPSHOT</version> 
<properties> 
    <java-version>1.7</java-version> 
    <org.springframework-version>3.2.0.RELEASE</org.springframework-version> 
    <org.aspectj-version>1.6.9</org.aspectj-version> 
    <org.slf4j-version>1.5.10</org.slf4j-version> 
    <org.hibernate-version>4.1.1.Final</org.hibernate-version> 
</properties> 
<dependencies> 

<!-- Aspect J --> 
<dependency> 
<groupId>aspectj</groupId> 
<artifactId>aspectjweaver</artifactId> 
<version>1.5.2</version> 
</dependency> 

<!-- commons - pool --> 
<dependency> 
<groupId>commons-pool</groupId> 
<artifactId>commons-pool</artifactId> 
<version>1.4</version> 
</dependency> 

    <!-- Spring --> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-context</artifactId> 
     <version>${org.springframework-version}</version> 
     <exclusions> 
      <!-- Exclude Commons Logging in favor of SLF4j --> 
      <exclusion> 
       <groupId>commons-logging</groupId> 
       <artifactId>commons-logging</artifactId> 
      </exclusion> 
     </exclusions> 
    </dependency> 

    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-webmvc</artifactId> 
     <version>${org.springframework-version}</version> 
    </dependency> 


    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-tx</artifactId> 
     <version>${org.springframework-version}</version> 
    </dependency> 

    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-orm</artifactId> 
     <version>${org.springframework-version}</version> 
    </dependency> 

    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-test</artifactId> 
     <version>${org.springframework-version}</version> 
    </dependency> 

    <!-- Spring security --> 
    <!-- <dependency> 
     <groupId>org.springframework.security</groupId> 
     <artifactId>spring-security-core</artifactId> 
     <version>${org.springframework-version}</version> 
    </dependency> 

    <dependency> 
     <groupId>org.springframework.security</groupId> 
     <artifactId>spring-security-web</artifactId> 
     <version>${org.springframework-version}</version> 
    </dependency> 

    <dependency> 
     <groupId>org.springframework.security</groupId> 
     <artifactId>spring-security-config</artifactId> 
     <version>${org.springframework-version}</version> 
    </dependency> --> 

    <!-- Spring Data --> 
    <dependency> 
     <groupId>org.springframework.data</groupId> 
     <artifactId>spring-data-jpa</artifactId> 
     <version>1.1.0.RELEASE</version> 
    </dependency> 

    <!-- Hibernate dependencies --> 
    <dependency> 
     <groupId>org.hibernate</groupId> 
     <artifactId>hibernate-core</artifactId> 
     <version>${org.hibernate-version}</version> 
    </dependency> 

    <dependency> 
     <groupId>org.hibernate</groupId> 
     <artifactId>hibernate-entitymanager</artifactId> 
     <version>${org.hibernate-version}</version> 
    </dependency> 

    <dependency> 
     <groupId>org.hibernate</groupId> 
     <artifactId>hibernate-ehcache</artifactId> 
     <version>${org.hibernate-version}</version> 
    </dependency> 

    <dependency> 
    <groupId>org.hibernate</groupId> 
    <artifactId>hibernate-validator</artifactId> 
    <version>4.2.0.Final</version> 
    </dependency> 


    <dependency> 
     <groupId>javassist</groupId> 
     <artifactId>javassist</artifactId> 
     <version>3.12.1.GA</version> 
    </dependency> 

    <dependency> 
     <groupId>commons-dbcp</groupId> 
     <artifactId>commons-dbcp</artifactId> 
     <version>1.4</version> 
    </dependency> 

    <dependency> 
     <groupId>cglib</groupId> 
     <artifactId>cglib</artifactId> 
     <version>2.2.2</version> 
     <scope>runtime</scope> 
    </dependency> 

    <!-- H2 database for testing --> 
    <dependency> 
     <groupId>com.h2database</groupId> 
     <artifactId>h2</artifactId> 
     <version>1.3.171</version> 
    </dependency> 

    <!-- Postgres Connector --> 
    <dependency> 
     <groupId>postgresql</groupId> 
     <artifactId>postgresql</artifactId> 
     <version>9.1-901.jdbc4</version> 
    </dependency> 

    <!-- AspectJ --> 
    <dependency> 
     <groupId>org.aspectj</groupId> 
     <artifactId>aspectjrt</artifactId> 
     <version>${org.aspectj-version}</version> 
    </dependency> 

    <!-- Logging --> 
    <dependency> 
     <groupId>org.slf4j</groupId> 
     <artifactId>slf4j-api</artifactId> 
     <version>${org.slf4j-version}</version> 
    </dependency> 

    <dependency> 
     <groupId>org.slf4j</groupId> 
     <artifactId>jcl-over-slf4j</artifactId> 
     <version>${org.slf4j-version}</version> 
     <scope>runtime</scope> 
    </dependency> 

    <dependency> 
     <groupId>org.slf4j</groupId> 
     <artifactId>slf4j-log4j12</artifactId> 
     <version>${org.slf4j-version}</version> 
     <scope>runtime</scope> 
    </dependency> 

    <dependency> 
     <groupId>log4j</groupId> 
     <artifactId>log4j</artifactId> 
     <version>1.2.15</version> 
     <exclusions> 
      <exclusion> 
       <groupId>javax.mail</groupId> 
       <artifactId>mail</artifactId> 
      </exclusion> 
      <exclusion> 
       <groupId>javax.jms</groupId> 
       <artifactId>jms</artifactId> 
      </exclusion> 
      <exclusion> 
       <groupId>com.sun.jdmk</groupId> 
       <artifactId>jmxtools</artifactId> 
      </exclusion> 
      <exclusion> 
       <groupId>com.sun.jmx</groupId> 
       <artifactId>jmxri</artifactId> 
      </exclusion> 
     </exclusions> 
     <scope>runtime</scope> 
    </dependency> 

    <!-- @Inject --> 
    <dependency> 
     <groupId>javax.inject</groupId> 
     <artifactId>javax.inject</artifactId> 
     <version>1</version> 
    </dependency> 

    <!-- Servlet --> 
    <dependency> 
     <groupId>javax.servlet</groupId> 
     <artifactId>servlet-api</artifactId> 
     <version>2.5</version> 
     <scope>provided</scope> 
    </dependency> 

    <dependency> 
     <groupId>javax.servlet.jsp</groupId> 
     <artifactId>jsp-api</artifactId> 
     <version>2.1</version> 
     <scope>provided</scope> 
    </dependency> 

    <dependency> 
     <groupId>javax.servlet</groupId> 
     <artifactId>jstl</artifactId> 
     <version>1.2</version> 
    </dependency> 

    <!-- Test --> 
    <dependency> 
     <groupId>junit</groupId> 
     <artifactId>junit</artifactId> 
     <version>4.7</version> 
     <scope>test</scope> 
    </dependency> 

    <!-- Common utilities --> 
    <dependency> 
     <groupId>org.apache.commons</groupId> 
     <artifactId>commons-lang3</artifactId> 
     <version>3.1</version> 
    </dependency> 

</dependencies> 

<build> 
    <plugins> 
     <plugin> 
      <artifactId>maven-eclipse-plugin</artifactId> 
      <version>2.9</version> 
      <configuration> 
       <additionalProjectnatures> 
        <projectnature>org.springframework.ide.eclipse.core.springnature</projectnature> 
       </additionalProjectnatures> 
       <additionalBuildcommands> 
        <buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand> 
       </additionalBuildcommands> 
       <downloadSources>true</downloadSources> 
       <downloadJavadocs>true</downloadJavadocs> 
      </configuration> 
     </plugin> 

     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-compiler-plugin</artifactId> 
      <version>2.3.2</version> 
      <configuration> 
       <source>${java-version}</source> 
       <target>${java-version}</target> 
       <compilerArgument>-Xlint:all</compilerArgument> 
       <showWarnings>true</showWarnings> 
       <showDeprecation>true</showDeprecation> 
      </configuration> 
     </plugin> 

     <plugin> 
      <groupId>org.codehaus.mojo</groupId> 
      <artifactId>exec-maven-plugin</artifactId> 
      <version>1.2.1</version> 
      <configuration> 
       <mainClass>org.test.int1.Main</mainClass> 
      </configuration> 
     </plugin> 
    </plugins> 
</build> 

的servlet上下文

<!-- DispatcherServlet Context: defines this servlet's request-processing 
    infrastructure --> 

<!-- Enables the Spring MVC @Controller programming model --> 
<annotation-driven /> 



<!-- Handles HTTP GET requests for /resources/** by efficiently serving 
    up static resources in the ${webappRoot}/resources directory --> 
<resources mapping="/resources/**" location="/resources/" /> 

<!-- Resolves views selected for rendering by @Controllers to .jsp resources 
    in the /WEB-INF/views directory --> 
<beans:bean 
    class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    <beans:property name="prefix" value="/WEB-INF/views/" /> 
    <beans:property name="suffix" value=".jsp" /> 
</beans:bean> 



<context:component-scan base-package="com.org.dtrack"> 
    <context:include-filter type="aspectj" 
     expression="com.org.dtrack.*" /> 
</context:component-scan> 

<beans:bean id="messageSource" 
    class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> 
    <beans:property name="basename" value="classpath:messages" /> 
    <beans:property name="defaultEncoding" value="UTF-8" /> 
</beans:bean> 

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

<beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" 
    destroy-method="close" p:driverClassName="${jdbc.driverClassName}" 
    p:url="${jdbc.databaseurl}" p:username="${jdbc.username}" p:password="${jdbc.password}" /> 


<beans:bean id="sessionFactory" 
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
    <beans:property name="dataSource" ref="dataSource" /> 
    <beans:property name="configLocation"> 
     <beans:value>classpath:hibernate.cfg.xml</beans:value> 
    </beans:property> 
    <beans:property name="hibernateProperties"> 
     <beans:props> 
     <!-- <beans:prop key="current_session_context_class">thread</beans:prop> --> 
      <beans:prop key="hibernate.dialect">${jdbc.dialect}</beans:prop> 
      <beans:prop key="hibernate.show_sql">true</beans:prop> 
     </beans:props> 
    </beans:property> 
</beans:bean> 

<!-- <tx:annotation-driven /> --> 
<beans:bean id="transactionManager" 
    class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 
    <beans:property name="sessionFactory" ref="sessionFactory" /> 
</beans:bean> 

的ContactController

@Controller 
public class ContactController { 

@Autowired 
public ContactService contactService; 

@RequestMapping("/index") 
public String listContacts(Map<String, Object> map) { 
    System.out.print("in index mapping"); 
    map.put("contact", new Contact()); 
    map.put("contactList", contactService.listContact()); 

    return "contact"; 
    } 
} 

ContactDAOImpl

@Repository 
public class ContactDAOImpl implements ContactDAO { 


    @Autowired 
public SessionFactory sessionFactory; 

@Transactional 
public void addContact(Contact contact) { 
    // TODO Auto-generated method stub 
    sessionFactory.getCurrentSession().save(contact); 

} 

@Transactional 
public List<Contact> listContact() { 
    // TODO Auto-generated method stub 
    return sessionFactory.getCurrentSession().createQuery("from Contact").list(); 
} 
@Transactional 
public void removeContact(Integer id) { 
    // TODO Auto-generated method stub 
    Contact contact = (Contact) sessionFactory.getCurrentSession().load(
      Contact.class, id); 
    if (null != contact) { 
     sessionFactory.getCurrentSession().delete(contact); 
    } 

    } 
} 

contactServiceImpl

@Service 
public class ContactServiceImpl implements ContactService{ 


@Autowired 
private ContactDAO contactDAO; 

@Transactional 
public void addContact(Contact contact) { 
    contactDAO.addContact(contact); 
} 

@Transactional 
public List<Contact> listContact() { 

    return contactDAO.listContact(); 
} 

@Transactional 
public void removeContact(Integer id) { 
     contactDAO.removeContact(id); 
    } 
} 
+0

請格式化代碼。您可以選擇多行,然後單擊{}按鈕以獲得格式化的部分。您也可以取出pom.xml文件,保留異常堆棧跟蹤的前8-10行。 – gkamal

回答

3

取消註釋此行Spring配置文件,它應該工作。沒有這個@Transactional註釋被忽略。你在開始的標籤來自mvc命名空間(在你的情況下是默認命名空間) - 其目的與此不同。所以你需要有兩個。

<!-- <tx:annotation-driven /> --> 
+0

+1這應該有所斬獲。有關此Spring事務配置的文檔,請參閱本文檔的第13.3.3節(http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/orm)。 HTML#ORM眠)。 –