2015-01-19 2 views
0

@ Repository 클래스 (@Repository 주석이 붙어 있음)에서 @Transactional을 사용하고 있습니다. 클래스에 삽입 작업을 일부 수행했으며 session.persist()를 사용하여 데이터를 저장했습니다. 하지만 내 프로젝트에서 작동하지 않습니다. 다른 프로젝트에서 동일한 구성을 사용했으며 persist가 정상적으로 작동합니다. [동일 종속성을 사용하지 않음]. 다음 여기이 내 ApplicationContext.xml내 프로그램에서 session.persist()가 작동하지 않는 이유는 무엇입니까?

<beans 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"><mvc:annotation-driven/><!-- Configures the annotation-driven Spring MVC Controller programming model. 
    Note that, with Spring 3.0, this tag works in Servlet MVC only! --><mvc:resources location="/resources/" mapping="/resources/**"/><mvc:interceptors><bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"><property name="paramName" value="lang"/></bean></mvc:interceptors><bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"><property name="defaultLocale" value="en"/></bean><!-- Loading the resource 
    <import resource="hibernateContext.xml" /> --></beans> 

입니다

<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>myownspring</groupId><artifactId>myownspring</artifactId><version>0.0.1-SNAPSHOT</version><packaging>war</packaging><dependencies><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>3.1.0</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>4.1.4.RELEASE</version></dependency><dependency><groupId>org.springframework.security</groupId><artifactId>spring-security-web</artifactId><version>3.2.5.RELEASE</version></dependency><dependency><groupId>org.springframework.security</groupId><artifactId>spring-security-config</artifactId><version>3.2.5.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-tx</artifactId><version>4.1.4.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-orm</artifactId><version>4.1.4.RELEASE</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.34</version></dependency><dependency><groupId>org.hibernate</groupId><artifactId>hibernate-validator</artifactId> 

내 pom.xml 파일이 내입니다 내 코드

입니다 디스패처-servlet.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" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:p="http://www.springframework.org/schema/p" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 

      http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
      http://www.springframework.org/schema/mvc 
      http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd 
      http://www.springframework.org/schema/context 
      http://www.springframework.org/schema/context/spring-context-4.0.xsd"> 

<!-- Declare a view resolver --> 

<!-- Activates various annotations to be detected in bean classes --> 
    <mvc:annotation-driven/> 



<!-- Scans the classpath for annotated components that will be auto-registered as Spring beans. 
    For example @Controller and @Service. Make sure to set the correct base-package--> 
<context:component-scan base-package="myownspring" />  

    <!-- view resolver for choosing view --> 
    <bean id="viewResolver" 
     class="org.springframework.web.servlet.view.InternalResourceViewResolver" 
     p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" /> 


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


    <context:property-placeholder location="classpath:hibernate.properties" /> 


    <!-- <bean id="multipartResolver" 
    class="org.springframework.web.multipart.commons.CommonsMultipartResolver" /> 
--> 

    <import resource="hibernateContext.xml" /> 
</beans> 

이것은 내 hibernate.context.xml 파일

입니다.
<beans xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"><!-- <context:property-placeholder location="/WEB-INF/config/hibernate.properties" /> --><!-- Enabling transaction annotiaon --><tx:annotation-driven/><!-- Defining the datasource using c3P0 --><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close" p:driverClass="${jdbc.driverClassName}" p:jdbcUrl="${jdbc.url}" p:user="${jdbc.username}" p:password="${jdbc.password}" p:acquireIncrement="1" p:idleConnectionTestPeriod="30" p:maxIdleTime="3600" p:maxPoolSize="100" p:maxStatements="2" p:minPoolSize="20"/><bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" p:packagesToScan="myownspring" p:dataSource-ref="dataSource"/><bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager" lazy-init="true"><property name="sessionFactory" ref="sessionFactory"/></bean></beans> 

이 내 web.xml 파일

<?xml version="1.0" encoding="UTF-8"?> 
<web-app id="WebApp_ID" version="3.0" 
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_3_0.xsd"> 
<display-name>SEDI-Portal</display-name> 

<!-- The definition of the Root Spring Container shared by all Servlets and Filters --> 

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

<!-- For Spring Security --> 

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

<!-- Creates the Spring Container shared by all Servlets and Filters (loads context parameters)--> 

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

<!-- Processes application requests --> 

<servlet> 
    <servlet-name>DispatcherServlet</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <!-- not required if you have put servlet-name-servlet.xml in WEB-INF folder --> 
    <init-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>/WEB-INF/config/DispatcherServlet-servlet.xml</param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
</servlet> 
<servlet-mapping> 
    <servlet-name>DispatcherServlet</servlet-name> 
    <url-pattern>/AS/*</url-pattern> 
</servlet-mapping> 


<welcome-file-list> 
    <welcome-file>/NewFile.html</welcome-file> 
</welcome-file-list> 
</web-app> 

입니다 그리고 이것은 내가 @Transactional 사용하고있는 내 수업 파일입니다

package myownspring.daoimpl; 

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

import myownspring.dao.CrudUserDao; 
import myownspring.model.User; 

import org.springframework.stereotype.Repository; 
import org.springframework.transaction.annotation.Transactional; 

@Repository 
public class CrudUserDaoImpl implements CrudUserDao { 

    @Autowired 
    SessionFactory sessionfactory; 

    Session session; 

    //@Override 
    @Transactional 
    public void createUser() { 
     // TODO Auto-generated method stub 
     session=sessionfactory.openSession(); 
     System.out.println("inside dao"); 
     User u=new User(); 
     u.setEmail("[email protected]"); 
     u.setPassword("gg"); 
     u.setRoleId(0); 
     session.persist(u); 
    } 



} 

이 내 모델 클래스입니다

package myownspring.model; 

import java.io.Serializable; 

import javax.persistence.*; 

import org.hibernate.annotations.Generated; 
import org.hibernate.validator.constraints.*; 

@Entity 
@Table(name="user") 
public class User implements Serializable{ 

    private int id; 
    private String email; 
    private String password; 
    private int roleId; 

    @Id 
    @GeneratedValue 
    public int getId() { 
     return id; 
    } 

    public void setId(int id) { 
     this.id = id; 
    } 
    @Column(name="username") 
    @NotEmpty 
    @Email 
    public String getEmail() { 
     return email; 
    } 

    public void setEmail(String email) { 
     this.email = email; 
    } 

    @Column(name="password") 
    @NotEmpty(message="not empty") 
    public String getPassword() { 
     return password; 
    } 

    public void setPassword(String password) { 
     this.password = password; 
    } 

    @Column(name="contact") 
    public int getRoleId() { 
     return roleId; 
    } 

    public void setRoleId(int roleId) { 
     this.roleId = roleId; 
    } 


} 

도움에 감사드립니다

+0

로그에 지속 작업이 실패하는 이유를 알리는 예외가 있습니까? –

+0

'session.openTransaction()'대신'session.getCurrentTransaction()'을 시도하십시오 –

+0

@kurt 저는 스프링을 사용하고 있습니다. 그래서 스프링이 트랜잭션을 처리하기를 원합니다. 이것이 제가 사용하는 이유입니다 /@Transactional.As I 내 comment.It에 다른 프로젝트에서 일하고있다 그리고 난 save() 메소드를 사용하고 싶지 않아. –

답변

1

sessionfactory.openSession()sessionfactory.getCurrentSession()으로 대체되어야한다고 생각합니다. 현재 세션은 Spring에서 열고 관리하는 세션이기 때문입니다. 다른 하나를 여는 것은 Spring 트랜잭션 관리를 우회하는 것이다.

관련 문제