EntityManager + @Transactional

时光总嘲笑我的痴心妄想 提交于 2019-12-10 17:14:48

问题


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

The transaction in service method save(Video video) is never started so also never commited. Where is the error? When I use EntityManagerFactory it works perfectly, but I don't want to explicitly begin and commit transaction. I want to use it with @Transactional annotation.


回答1:


@beerbajay is correct, @Transactional will need a dynamic proxy to be created on your bean to apply the transactional logic, which can be created if your Service has an interface, since in your case it doesn't an alternate would be to instruct Spring to create class based proxy, the following way:

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


来源:https://stackoverflow.com/questions/11015854/entitymanager-transactional

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!