问题描述
我正在尝试将spring xml配置转换为java配置.通过XML配置,它可以完美地工作.但是,如果我使用java config初始化程序,它将引发以下异常.尝试运行JQL时会发生这种情况.不过,该应用程序可以正常启动(已初始化所有JPA映射).
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
以下是我的持久性初始化程序类.一些阅读提示我这与事务未正确启动有关.我已经将调试点放置到这些方法中的每一个上,但是在服务器启动期间或以后的任何时间都不会执行transactionManager方法.我不确定我在做什么错了:(.当通过persistence.xml初始化持久性时,基于相同代码的代码可以完美地工作.
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;
}
在Dao上访问此方法时发生错误
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();
}
推荐答案
我遇到了一些问题,我通过在执行删除或更新的服务方法上添加@Transactional批注来解决了该问题.就我而言,这是一个调用存储库方法的方法,该方法通过jpql执行删除,就像这样,我认为它也可以解决您的问题:
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);
这篇关于InvalidDataAccessApiUsageException:对Java配置执行更新/删除查询Spring XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!