简体   繁体   中英

JPQL update query not working in spring boot application

I am trying to update Student emailId based on the firstName as following.

@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);

My test code

 @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);
 }

I get the following output for my Test code

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

But cannot see the updated changes in the MySQL database. Corresponding native query also not working. How can I fix this?

在您的查询中添加 nativeQuery = true,尝试使用下面的 @Query

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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