JPQL更新查询在spring启动应用程序中不起作用



我正在尝试根据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)

相关内容

  • 没有找到相关文章

最新更新