Spring Data JPA 审计不适用于带有 @Modifying 批注的 JpaRepository 更新方法,为什么? [英] Spring Data JPA Auditing not working for the JpaRepository update method with @Modifying annotation, why?

查看:21
本文介绍了Spring Data JPA 审计不适用于带有 @Modifying 批注的 JpaRepository 更新方法,为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究 Spring Data JPA 和 Postgres 示例.在这个例子中,我通过以下链接实现了Auditing:https://www.baeldung.com/database-auditing-jpaSpring Boot JPA@CreatedDate @LastModifiedDate 保存对象时未填充.审计工作非常好当我执行 repository.save 时,在这种情况下,用 @CreatedDate@LastModifiedDate 注释的两个字段都正确保存.

I am working on Spring Data JPA and Postgres example. In this example, I've implemented Auditing by following link: https://www.baeldung.com/database-auditing-jpa and Spring Boot JPA@CreatedDate @LastModifiedDate not being populated when saving the object. Auditing working very fine When I do the repository.save, in this case both fields annotated with @CreatedDate and @LastModifiedDate are saving correctly.

但是当我尝试更新方法时不会发生同样的事情.

But same is not happening when I'm trying to update the method.

我开发了以下方法.

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@EntityListeners(AuditingEntityListener.class)
@Entity
@Table(uniqueConstraints = {
        @UniqueConstraint(name="student_name_key",columnNames = {"studentName"})
})
public class Student {
    ....
    ....
    @Column(name="lastUpdateUser")
    private String lastUpdateUser;

    @LastModifiedDate
    @Column(name="lastUpdateDate", nullable = false)
    private LocalDateTime lastUpdateDate; 
}

主应用

@SpringBootApplication
@EnableJpaAuditing
@EnableJpaRepositories(basePackages = {"com.xxx.xxx.repository"})
@ComponentScan(basePackages = {"com.xxx.yyy","com.xxx.xxx.studentportfolio"})
@EnableCaching
@EnableAsync
@EnableAspectJAutoProxy(proxyTargetClass = true)
@EnableAutoConfiguration(exclude = {ErrorMvcAutoConfiguration.class, SecurityAutoConfiguration.class})
public class MainApplication extends SpringBootServletInitializer implements CommandLineRunner{

    public static void main(String[] args) {
        SpringApplication.run(ProgramApplication.class, args);
    }
}

StudentRepository.java

public interface StusentRepository extenss JpaRepository<Stusent, Long>{

    @Mosifying(clearAutomatically = true)
    @Query("UPDATE Stusent s SET s.studentDescription=:stuDesc, s.studentId=:studentId, s.sivisionCode=:cd, "
            + "s.status=:status WHERE s.studentName=:stuName")
    vois upsateStudent(@Param("stuName") String studentName,
                        @Param("stuDesc") String studentDescription,
                        @Param("studentId") String studentId,
                        @Param("cd") String cd,
                        @Param("status") String status);
}

推荐答案

审计基于 JPA 生命周期事件.只有直接操作实例的方法(persistmergeremove)才会触发此类事件.

Auditing is based on the JPA Lifecycle events. Only the methods directly manipulating instances (persist, merge and remove) trigger such events.

查询的执行、修改或其他方式不会触发任何事件,因此不会导致审计发生.

The execution of queries, modifying or otherwise, does not trigger any events and therefore, won't cause auditing to happen.

有关详细信息,请参阅 JPA 规范部分 3.5.2 生命周期方法.

See the JPA Specification section 3.5.2 Lifecycle Methods for details.

这篇关于Spring Data JPA 审计不适用于带有 @Modifying 批注的 JpaRepository 更新方法,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆