2012-06-13 6 views
3
@Service 
@Repository 
@Transactional 
public class VideoService { 

    @PersistenceContext 
    EntityManager entityManager; 

    public void save(Video video) { 

     Video video1 = new Video(); 

     entityManager.persist(video1); 

    } 



<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0"> 
    <persistence-unit name="video_pu" transaction-type="RESOURCE_LOCAL" > 
     <provider>org.hibernate.ejb.HibernatePersistence</provider> 

     <properties> 
      <property name="hibernate.hbm2ddl.auto" value="create-drop" /> 
      <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" /> 
     </properties> 

    </persistence-unit> 
</persistence> 


<bean id="dataSource" 
     class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
     <property name="driverClassName" value="com.mysql.jdbc.Driver" /> 
     <property name="url" value="jdbc:mysql://localhost/video" /> 
     <property name="username" value="root" /> 
     <property name="password" value="root" /> 
    </bean>  

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
     <property name="persistenceUnitName" value="video_pu"/> 
     <property name="dataSource" ref="dataSource" />  
     <property name="jpaVendorAdapter"> 
     <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> 
      <property name="showSql" value="true" /> 
      <property name="generateDdl" value="true" /> 
      <property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect" /> 
     </bean> 
    </property> 

    </bean> 



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

    <!-- enable the configuration of transactional behavior based on annotations --> 
    <tx:annotation-driven transaction-manager="transactionManager"/> 


    <!-- post-processors for all standard config annotations --> 
    <context:annotation-config/> 

트랜잭션 서비스 메서드 save (비디오 비디오)는 절대로 시작되지 않으므로 커밋되지 않습니다. 오류는 어디에 있습니까? EntityManagerFactory를 사용할 때 완벽하게 작동하지만 명시 적으로 트랜잭션을 시작하고 커밋하고 싶지 않습니다. @Transactional 주석과 함께 사용하고 싶습니다.EntityManager + @Transactional

+0

오류가 있습니까? –

+0

나는 트랜잭션 주석을 적용 할 인터페이스를 사용해야한다고 확신한다. 'VideoService'와'VideoServiceImpl' – beerbajay

답변

2

@beerbajay가 맞습니다. @Transactional은 트랜잭션 로직을 적용하기 위해 bean에 동적 프록시를 만들어야합니다. 서비스에 인터페이스가있는 경우 만들 수 있습니다. 다음과 같은 방법으로 클래스 기반 프록시를 생성하도록 Spring에 지시해야합니다.

<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class='true/> 
+0

No XML 설정에서 어떻게 이것을 할 수 있습니까? – Jess