簡體   English   中英

JPQL 更新查詢在 Spring Boot 應用程序中不起作用

[英]JPQL update query not working in spring boot application

我正在嘗試根據 firstName 更新 Student emailId,如下所示。

@Modifying(clearAutomatically = true)//, flushAutomatically = true)
@Query(value = "update Student s set s.emailId=:email where s.firstName =:firstName and s.id>0")
public int updateStudentEmailByFirstName(@Param("firstName") String firstName, @Param("email") String email);

我的測試代碼

 @Test
 @Order(11)
 @Transactional
 public void updateEmailByFirstName() {
     String firstName = "sm";
     String email = "sm2@gmail.com";
     //studentRepository.setSafeUpdateFalse();
     int res = studentRepository.updateStudentEmailByFirstName(firstName, email);
     //studentRepository.setSafeUpdateTrue();
     System.out.println("res = " + res);
 }

我的測試代碼得到以下輸出

2022-07-13 11:27:54.693  INFO 12932 --- [           main] o.s.t.c.transaction.TransactionContext   : Began transaction (1) for test context [DefaultTestContext@1f05d08c testClass = StudentRepositoryTest, testInstance = com.sm.jpa.demo.repository.StudentRepositoryTest@2c6d442d, testMethod = updateEmailByFirstName@StudentRepositoryTest, testException = [null], mergedContextConfiguration = [WebMergedContextConfiguration@71842e18 testClass = StudentRepositoryTest, locations = '{}', classes = '{class com.sm.jpa.demo.JpaDemoApplication}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}', contextCustomizers = set[org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@49912c99, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@5f9b2141, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@10feca44, org.springframework.boot.test.autoconfigure.actuate.metrics.MetricsExportContextCustomizerFactory$DisableMetricExportContextCustomizer@3c73951, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@71def8f8, org.springframework.boot.test.context.SpringBootTestArgs@1, org.springframework.boot.test.context.SpringBootTestWebEnvironment@17f6480], resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]], attributes = map['org.springframework.test.context.web.ServletTestExecutionListener.activateListener' -> true, 'org.springframework.test.context.web.ServletTestExecutionListener.populatedRequestContextHolder' -> true, 'org.springframework.test.context.web.ServletTestExecutionListener.resetRequestContextHolder' -> true, 'org.springframework.test.context.event.ApplicationEventsTestExecutionListener.recordApplicationEvents' -> false]]; transaction manager [org.springframework.orm.jpa.JpaTransactionManager@4bab804f]; rollback [true]
Hibernate: update tbl_student set email_address=? where first_name=? and id>0
res = 1

但是在 MySQL 數據庫中看不到更新的變化。 相應的本機查詢也不起作用。 我怎樣才能解決這個問題?

在您的查詢中添加 nativeQuery = true,嘗試使用下面的 @Query

@Query(value = "update Student s set s.emailId=:email where s.firstName =:firstName and s.id>0",nativeQuery = true)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM