java spring xml提示,InvalidDataAccessApiUsageException:对Java配置执行更新/删除查询Spring XML...

I'm trying to convert spring xml configuration to java configuration. This works perfectly through XML configuration. However, it throws the following exception if I use java config initializer. This happens when it tries to run JQL. The application starts properly though (with all JPA mapping initialized).

org.springframework.dao.InvalidDataAccessApiUsageException: Executing an update/delete query; nested exception is javax.persistence.TransactionRequiredException: Executing an update/delete query

at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:410) [spring-orm-4.1.5.RELEASE.jar:4.1.5.RELEASE]

at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:216) [spring-orm-4.1.5.RELEASE.jar:4.1.5.RELEASE]

at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:417) [spring-orm-4.1.5.RELEASE.jar:4.1.5.RELEASE]

at org.springframework.dao.support.ChainedPersistenceExceptionTranslat

Following is my persistence initializer class. Bit of reading suggested me this is related to transactions are not being started properly. I've put debug points to each of these methods but transactionManager method never gets executed during server startup or any later time. I'm not sure what am I doing wrong :(. Same code based works perfectly when persistence is initialized through persistence.xml.

@Configuration

@EnableTransactionManagement

@EnableJpaRepositories(basePackages = "au.mypkg")

public class DatabaseConfig {

@Bean(name = "dataSource")

@Primary

public DataSource dataSource() throws Exception {

Context ctx = new InitialContext();

return (DataSource) ctx.lookup("java:jboss/datasources/mydb");

}

@PersistenceContext(unitName = "persistenceUnit")

@Primary

@Bean(name = "entityManagerFactory")

public LocalContainerEntityManagerFactoryBean entityManagerFactory() throws Exception {

..........

@Primary

@Bean(name = "transactionManager")

public PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {

final JtaTransactionManager transactionManager = new JtaTransactionManager();

transactionManager.setTransactionManagerName(JBOSS_TRANSACTION_MANANGER);

return transactionManager;

}

Error occurs when accessing this method on Dao

public void updateById(final Long id) {

final String sqlQuery = "UPDATE testtable w SET w.LAST_ACCESSED = :date WHERE w.testtable_ID = :testid";

final Query query = dao.createNativeQuery(sqlQuery);

query.setParameter("date", new Date());

query.setParameter("testid", id);

query.executeUpdate();

}

解决方案

I had the some problem and I resolved it by just adding @Transactional annotation on the service method that perform delete or update.

In my case it was a method that call a repository method which execute a delete by jpql like this by I think it can solve you problem too:

@Modifying

@Query("delete from OtherPayment otherPayment " +

"where otherPayment.otherPaymentParam.id = :otherPaymentParamId")

void deleteByOtherPaymentParamId(@Param("otherPaymentParamId") Long otherPaymentParamId);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值