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

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

问题描述

我正在研究 Spring Data JPA和Postgres 示例。在此示例中,我通过以下链接实现了审核 https://www.baeldung.com/database-auditing-jpa 。审核工作非常好当我执行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.

I已经开发出以下方法。

I've developed following 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; 
}

Main.App

@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生命周期事件。
只有直接操作实例的方法(永久合并删除)触发此类事件。

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 Auditing无法用于带有@Modifying批注的JpaRepository更新方法,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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