与 Spring e Jpa 的交易
我有同样的问题,我的事务不写进入数据库。我认为这个Spring问题不是框架的错误,而是配置文件的问题(applicationContext.xml以便理解)所以我把我的配置文件:
...
<!-- this is a bridge for entityManager and PersistenceUnit -->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="persistenceUnitName" value="fb-persistence" />
</bean>
<bean id="myTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<bean name="pluginDaoImpl" class="it.synclab.fb.jpa.dao.impl.PluginDaoImpl" />
</beans>
一个接口是:
public interface PluginDao {
public Plugin load (int id);
public void save(Plugin plg);
}
一个接口的实现是:
public class PluginDaoImpl implements PluginDao {
@PersistenceContext (unitName="fb-persistence")
private EntityManager em;
public void setEntityManager(EntityManager entityManager) {
this.em = entityManager;
}
@Transactional
public Plugin load(int id) {
return em.find(Plugin.class, id);
}
@Transactional(readOnly=false, propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
public void save(Plugin plg) {
em.persist(plg);
//em.flush();
}
}
和dulcis在fundo中,我的PluginTest(用于脏测试)是:
public class PluginTest{
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
PluginDao dao = (PluginDao) applicationContext.getBean("pluginDaoImpl");
Plugin plugin1 = new Plugin();
//inserisco un nuovo plugin
//plugin.setId(14);
plugin1.setMethod("prova");
plugin1.setDescrizione("questa Bean Class!");
dao.save(plugin1);
Plugin plugin = new Plugin();
//carico un plugin per id
plugin = dao.load(9);
System.out.println("id: " + plugin.getId() +
" Descrizione: " + plugin.getDescrizione() +
" Method: " + plugin.getMethod());
}
如果我现在添加方法 PluginDaoImpl.save (), em.flush 行代码,我会收到此错误:
Exception in thread "main" javax.persistence.TransactionRequiredException: no transaction is in progress *
*未指定原因。
i have the same problem, my Transactions not write into DB. I think this Spring problem regard is not a bug of framework, but is a problem of configuration file (applicationContext.xml for understand) so I put my configuration file:
...
<!-- this is a bridge for entityManager and PersistenceUnit -->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="persistenceUnitName" value="fb-persistence" />
</bean>
<bean id="myTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<bean name="pluginDaoImpl" class="it.synclab.fb.jpa.dao.impl.PluginDaoImpl" />
</beans>
a interface is:
public interface PluginDao {
public Plugin load (int id);
public void save(Plugin plg);
}
a implementation of interface is:
public class PluginDaoImpl implements PluginDao {
@PersistenceContext (unitName="fb-persistence")
private EntityManager em;
public void setEntityManager(EntityManager entityManager) {
this.em = entityManager;
}
@Transactional
public Plugin load(int id) {
return em.find(Plugin.class, id);
}
@Transactional(readOnly=false, propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
public void save(Plugin plg) {
em.persist(plg);
//em.flush();
}
}
and dulcis in fundo, my PluginTest (for dirty-testing) is:
public class PluginTest{
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
PluginDao dao = (PluginDao) applicationContext.getBean("pluginDaoImpl");
Plugin plugin1 = new Plugin();
//inserisco un nuovo plugin
//plugin.setId(14);
plugin1.setMethod("prova");
plugin1.setDescrizione("questa Bean Class!");
dao.save(plugin1);
Plugin plugin = new Plugin();
//carico un plugin per id
plugin = dao.load(9);
System.out.println("id: " + plugin.getId() +
" Descrizione: " + plugin.getDescrizione() +
" Method: " + plugin.getMethod());
}
If I now add the method PluginDaoImpl.save (), em.flush line-code I get this error:
Exception in thread "main" javax.persistence.TransactionRequiredException: no transaction is in progress *
*the cause is not specificated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您缺少
在你的 context.xml 中
(参见 使用
@Transactional
)添加 tx 命名空间,如下所示:
You are missing
<tx:annotation-driven transaction-manager="myTransactionManager"/>
in your context.xml
(See Using
@Transactional
)Add the tx namespace like this: