简体   繁体   English

如何在JpaRepository上运行自定义更新查询?(TransactionRequiredException)

[英]How to run custom update query on JpaRepository?(TransactionRequiredException)

I am trying to write 2 native SQL queries in spring, 我正在尝试在春季编写2个本机SQL查询,

My query annotation with SQL in service class is: 我在服务类中使用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();
    }

But I received this error: 但我收到此错误:

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)

If I use entityManager.getTransaction().begin(); 如果我使用entityManager.getTransaction().begin(); and entityManager.getTransaction().commit(); entityManager.getTransaction().commit();

then its throwing : 然后将其抛出:

2019-09-12 18:36:00,007 ERROR [] osssTaskUtils$LoggingErrorHandler:95 - Unexpected error occurred in scheduled task. 2019-09-12 18:36:00,007错误[] osssTaskUtils $ LoggingErrorHandler:95-计划任务发生意外错误。 java.lang.IllegalStateException: Not allowed to create transaction on shared EntityManager - use Spring transactions or EJB CMT instead at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManager java.lang.IllegalStateException:不允许在共享EntityManager上创建事务-使用Spring事务或EJB CMT代替org.springframework.orm.jpa.SharedEntityManagerCreator $ SharedEntityManager

Why do I get this error? 为什么会出现此错误?

Or is there any other better way to implement custom update query with Jpa Repository ? 还是有其他更好的方法可以使用Jpa Repository实现自定义更新查询?

  1. I assume Spring will not create a transaction proxy since your method is not public . 我认为Spring 将不会创建事务代理,因为您的方法不是public
  2. You should always try to avoid manual transaction management if you are using declarative Spring Transaction Manager 如果您使用声明式Spring Transaction Manager,则应始终尝试避免手动进行事务管理。
  3. Your application should have a separate layer that handles DB calls . 您的应用程序应该有一个单独的层来处理数据库调用 In your case, it's JPA repositories. 在您的情况下,它是JPA存储库。 So it's better to implement a query in a custom Jpa repository, inject its instance here and invoke appropriate method. 因此,最好在自定义Jpa存储库中实现查询,在此处注入其实例并调用适当的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM