2012-04-03 2 views
1

나는 JPA와 함께 Hibernate를 사용한다. 나는 @Transactional 주석 클래스가이 클래스에서 나는 일부 개체를 지속 :@Transactional 및 entityManager.persist (o);

@Transactional 
@Repository 
public class ImageRepository { 
    @Autowired 
    private EntityManager em; 

    public Image factory(String imageHash) { 
     Image image = new Image(); 
     image.setHash(imageHash); 
     em.persist(image); 
     return image; 
    } 
} 

그 최대 절전 모드가 select nextval ('hibernate_sequence')으로 이미지 객체 ID를 할당 볼 수 있습니다. 하지만 그게 전부입니다. 개체가 데이터베이스에 넣어되지 않습니다 내가 em.flush();를 실행하려고하면 나는 점점 오전 :

javax.persistence.TransactionRequiredException: no transaction is in progress 

이 또한 내가 오류를 받고 있지 않다.

내 응용 프로그램 컨텍스트 :

<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" 
     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/tx 
      http://www.springframework.org/schema/tx/spring-tx-3.1.xsd"> 

    <context:annotation-config/> 
    <context:spring-configured/> 
    <context:property-placeholder location="classpath:application.properties"/> 

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

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

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
     <property name="persistenceUnitName" value="persistenceUnit"/> 
     <property name="jpaProperties"> 
      <props> 
       <prop key="hibernate.dialect">${database.dialect}</prop> 
       <prop key="hibernate.hbm2ddl.auto">${database.structure}</prop> 
       <prop key="hibernate.connection.url">${database.connection}</prop> 
       <prop key="hibernate.connection.username">${database.username}</prop> 
       <prop key="hibernate.connection.password">${database.password}</prop> 
       <prop key="hibernate.connection.driver_class">${database.driver}</prop> 
       <prop key="hibernate.connection.shutdown">true</prop> 
       <prop key="hibernate.connection.writedelay">0</prop> 
       <prop key="hibernate.show_sql">${database.show_sql}</prop> 
      </props> 
     </property> 
    </bean> 

    <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" /> 
</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="persistenceUnit" transaction-type="RESOURCE_LOCAL"> 
     <provider>org.hibernate.ejb.HibernatePersistence</provider> 
    </persistence-unit> 
</persistence> 

같은 일이 발생.

감사합니다.

편집 3 :

(이전 편집 관련되지 않은) 나는 지속되거나 DWR (DirectWebRemoting) 서블릿에서 오브젝트를 편집 할 때, 나는 아무 문제가 없다는 것을 발견

. 내 서블릿에서 동일한 작업을 시도 할 때 문제를 언급하고 있습니다. 하지만 내 서블릿의 모든 콩은 컴포넌트 스캔과 @Component 또는 @Repository와 같은 주석으로 Spring에서 만들어 지므로 어떤 점도 보이지 않습니다. 왜 그런 문제가 발생해야합니까?

답변

2

@Transactional은 봄의 일부입니다. 코드가 Spring context을 사용하지 않는 것 같습니다.

@Transactional 주석이 작동하는 것은 봄 문맥에 의해 포장왔다 필요는 ImageRepository.factory() 방법을 사용하십시오.

+0

결국 applicationContext가 아닌 servlet-applicationContext에서만 을 발견했습니다. 이제 제대로 작동합니다. 힌트를 가져 주셔서 감사합니다! –

관련 문제