spring 中的@Transactional 只读但不写
@Transactional in spring working for read only but not write
以前,我曾经明确地开始并提交事务,但现在我试图在 spring 中使用 @Transactional
注释。出于某种原因,所有只读操作都在工作,但 persist()
和 merge()
操作失败,我在日志中看不到任何内容。在调试时我还看到 spring 已经创建了代理但不确定我哪里出错了。这是我的代码和 spring 应用程序上下文配置。
1.Jpa-context.xml
<context:annotation-config />
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="PersistenceUnit" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<tx:annotation-driven proxy-target-class="true" transaction-manager="transactionManager" />
2.Framework-context.xml
<context:component-scan base-package="com.github.djuloori.whiteboard.framework" />
<context:annotation-config/>
<tx:annotation-driven proxy-target-class="true" transaction-manager="transactionManager" />
<bean id = "m_SecurableEntityManager" class = "com.github.djuloori.whiteboard.framework.SecurableEntityManagerImpl"/>
3.SecurableEntityManagerImpl.Java
@Service
@Transactional
public class SecurableEntityManagerImpl implements SecurableEntityManager {
@Autowired
private EntityManagerFactory entityManagerFactory;
public void save(Object var1) {
getEntityManager().persist(var1);
}
public <T> TypedQuery<T> createQuery(String var1, Class<T> var2) {
return getEntityManager().createNamedQuery(var1,var2);
}
public <T> T update(T var1) {
return getEntityManager().merge(var1);
}
private EntityManager getEntityManager(){
return entityManagerFactory.createEntityManager();
}
}
我也在阅读与 @transactional
相关的其他问题,但没有找到合适的答案。我是spring的新手,如果有人能帮助我就太好了。
自动装配 EntityManager 修复了问题
以前,我曾经明确地开始并提交事务,但现在我试图在 spring 中使用 @Transactional
注释。出于某种原因,所有只读操作都在工作,但 persist()
和 merge()
操作失败,我在日志中看不到任何内容。在调试时我还看到 spring 已经创建了代理但不确定我哪里出错了。这是我的代码和 spring 应用程序上下文配置。
1.Jpa-context.xml
<context:annotation-config />
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="PersistenceUnit" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<tx:annotation-driven proxy-target-class="true" transaction-manager="transactionManager" />
2.Framework-context.xml
<context:component-scan base-package="com.github.djuloori.whiteboard.framework" />
<context:annotation-config/>
<tx:annotation-driven proxy-target-class="true" transaction-manager="transactionManager" />
<bean id = "m_SecurableEntityManager" class = "com.github.djuloori.whiteboard.framework.SecurableEntityManagerImpl"/>
3.SecurableEntityManagerImpl.Java
@Service
@Transactional
public class SecurableEntityManagerImpl implements SecurableEntityManager {
@Autowired
private EntityManagerFactory entityManagerFactory;
public void save(Object var1) {
getEntityManager().persist(var1);
}
public <T> TypedQuery<T> createQuery(String var1, Class<T> var2) {
return getEntityManager().createNamedQuery(var1,var2);
}
public <T> T update(T var1) {
return getEntityManager().merge(var1);
}
private EntityManager getEntityManager(){
return entityManagerFactory.createEntityManager();
}
}
我也在阅读与 @transactional
相关的其他问题,但没有找到合适的答案。我是spring的新手,如果有人能帮助我就太好了。
自动装配 EntityManager 修复了问题