我正在尝试在春季编写2个本机SQL查询,
我在服务类中使用SQL的查询注释是:
@Transactional
@Scheduled(cron = "0 0/1 * * * *")
void testMethod() {
String query = "UPDATE BUDGET_GROUP_SPEND_SUMMARY BSS, USER U, EMPLOYEE_MOVE_STAGE EMS, BUDGET B, SERVICE_GROUP SG " +
"SET BSS.BULK_RECALC_LOCK = true " +
"WHERE BSS.EMPLOYEE_ID = U.USER_ID " +
"AND U.USER_ID = EMS.USER_ID " +
"AND BSS.BUDGET_GROUP_ID = B.BUDGET_ID " +
"AND B.BUDGET_SERVICE_GROUP_ID IS NOT NULL " +
"AND EMS.MOVE_STAGE_ID IN (1,2) " +
"AND U.USER_CUSTOMER_STATUS IN ('MOVE_COMPLETED', 'LOST', 'GET_FEEDBACK', 'CLOSED_NO_FEEDBACK', 'ON_ASSIGNMENT') " +
"AND B.BUDGET_SERVICE_GROUP_ID = SG.SERVICE_GROUP_ID " +
"AND SG.MOVE_STAGE_ID = 1";
Query nq = entityManager.createNativeQuery(query);
nq.executeUpdate();
}
但是我收到此错误:
2019-09-12 18:28:00,012 ERROR [] o.s.s.s.TaskUtils$LoggingErrorHandler:95 - Unexpected error occurred in scheduled task.
javax.persistence.TransactionRequiredException: Executing an update/delete query
at org.hibernate.jpa.spi.AbstractQueryImpl.executeUpdate(AbstractQueryImpl.java:54)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
如果我使用entityManager.getTransaction().begin();
和entityManager.getTransaction().commit();
然后将其抛出:
2019-09-12 18:36:00,007错误[] o.s.s.s.TaskUtils $ LoggingErrorHandler:95-发生意外错误 在预定任务中。 java.lang.IllegalStateException:不允许 在共享的EntityManager上创建事务-使用Spring事务 或EJB CMT代替 在org.springframework.orm.jpa.SharedEntityManagerCreator $ SharedEntityManager
为什么会出现此错误?
或者还有其他更好的方法可以用Jpa Repository
实现自定义更新查询吗?
答案 0 :(得分:1)
public
。