EntityManager cannot use persist to save element to database

白昼怎懂夜的黑 提交于 2019-11-29 10:42:42

To enable @Transactional in your Spring context you should have the following:

Appropriate for your version of Spring:

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    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.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

Enable the annotations:

<tx:annotation-driven />

Declare your transaction manager injecting your entity manager:

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

If you still have this problem and all the configurations are ok, please make sure that the @Transaction annotated method is public, not protected in order to be identified/managed by the transaction manager.

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