복붙노트

[SPRING] Spring - JPA - 작업을 읽지 만 계속 유지합니다. 사용할 수있는 트랜잭션 EntityManager가 없습니다. 왜?

SPRING

Spring - JPA - 작업을 읽지 만 계속 유지합니다. 사용할 수있는 트랜잭션 EntityManager가 없습니다. 왜?

Spring과 JPA로 응용 프로그램을 구축하고 있습니다. 내 읽기 함수가 작동하지만 어떤 개체를 저장하려고하면 No Transactional EntityManager를 사용할 수 있습니다.

다음은 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">

    <!-- The definition of the Root Spring Container shared by all Servlets 
        and Filters -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/root-context.xml,/WEB-INF/spring/webservices-context.xml,/WEB-INF/spring/security-context.xml</param-value>

    </context-param>

    <!-- Creates the Spring Container shared by all Servlets and Filters -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

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

    <mime-mapping>
        <extension>js</extension>
        <mime-type>application/x-javascript</mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>properties</extension>
        <mime-type>application/text</mime-type>
    </mime-mapping>


    <!-- Processes application requests -->
    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

다음은 Spring / appServlet / servlet-context.xml입니다.

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

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

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

    <beans:bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <beans:property name="location" value="classpath:resources.properties"></beans:property>
    </beans:bean>

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
    <resources mapping="/**" location="${resources.base},  ${resources.modules}, ${resources.lib}, ${resources.apps}" /> <!-- resource mapp for extjs and Designer to / -->
    <resources mapping="/resources/**" location="${resources.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.app.webapp" />
</beans:beans>

여기 내 spring / root-context.xml이 있습니다 :

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
        xmlns:aop="http://www.springframework.org/schema/aop"
    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-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/mvc 
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <!-- Root Context: defines shared resources visible to all other web components -->
    <context:annotation-config />

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

    <bean id="appDS"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource"
        p:driverClassName="com.mysql.jdbc.Driver" p:url="jdbc:mysql://localhost/schema1"
        p:username="user1" p:password="741147">
    </bean>

    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="appDS" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
        </property>
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager" />

    <bean id="persistenceExceptionTranslationPostProcessor"
        class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
    <context:spring-configured />
    <context:annotation-config />
</beans>

다음은 내 persistence.xml입니다.

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
  <persistence-unit name="myPU" transaction-type="JTA">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <properties>
      <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
      <property name="hibernate.transaction.auto_close_session" value="false"/>
      <property name="hibernate.show_sql" value="true"/>
      <property name="hibernate.format_sql" value="false"/>
      <property name="hibernate.generate_statistics" value="false"/>
      <property name="hibernate.connection.autocommit" value="true"/>
<!--       <property name="hibernate.hbm2ddl.auto" value="update"/> -->
      <property name="hibernate.query.jpaql_strict_compliance" value="false"/>
      <property name="hibernate.transaction.jta.platform"  value="org.hibernate.service.jta.platform.internal.SunOneJtaPlatform" />
      <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
    </properties>
  </persistence-unit>
</persistence>

여기에 내 DaoImpl.java입니다 :

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.app.webapp.data.dao.impl.GenericDaoImpl;
import com.app.webapp.data.dao.interfaces.security.IUserDao;
import com.app.webapp.data.domains.entities.Account;
import com.app.webapp.data.domains.entities.security.AccessPrivilege;
import com.app.webapp.data.domains.entities.security.ActiveSession;
import com.app.webapp.data.domains.entities.security.Process;
import com.app.webapp.data.domains.entities.security.Profile;
import com.app.webapp.data.domains.entities.security.User;
import com.app.webapp.data.exceptions.DuplicatedUserException;
import com.app.webapp.data.exceptions.UserAlreadyHasAnAccountException;
import com.app.webapp.data.exceptions.UserNotFoundException;

    @Repository("userDao")
    @Transactional
    public class UserDaoImpl implements IUserDao {

    @PersistenceContext(unitName = "myPU")
        protected EntityManager entityManager;

        public UserDaoImpl() {
            super(User.class);
        }

        @Override
        @Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
        public void save(User user) throws DuplicatedUserException {
            logger.info("Creating User with username: " + user.getUsername());
            User foundUser = null;
            logger.info("Checking if user already existis...");
            entityManager.persist(user);
            logger.info("User " + user.getUsername() + " was been saved with success!");

        }
    }

그런 다음 사용자를 지속시키는 줄이 실행되고 예외가 발생합니다.

하지만 왜 내가 DB에서 데이터를 얻을 수 있지만 쓸 수 없다는 것을 이해할 수 없다. 서블릿 컨텍스트에 무엇이 있는지, 다른 컨텍스트와 공유되지 않는지 나는 알 수 없습니다. 누군가 나를 도울 수 있습니까?

해결법

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

    1.@Service 또는 서비스 계층 클래스에서 @Transactional Annotation을 제공 했습니까? 그렇다면 hibernet / entityManager.jar 버전 호환성을 확인하십시오.

    @Service 또는 서비스 계층 클래스에서 @Transactional Annotation을 제공 했습니까? 그렇다면 hibernet / entityManager.jar 버전 호환성을 확인하십시오.

  2. from https://stackoverflow.com/questions/27277803/spring-jpa-reads-work-but-persist-give-me-no-transactional-entitymanager-ava by cc-by-sa and MIT license