Spring Data JPA Auditing not working for the JpaRepository update method with @Modifying annotation, why?
Asked Answered
G

3

9

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'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);
}
Gilbertgilberta answered 30/6, 2019 at 9:21 Comment(0)
D
8

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.

See the JPA Specification section 3.5.2 Lifecycle Methods for details.

Dipole answered 1/7, 2019 at 4:56 Comment(1)
so there is no way to persist jpq query result in audit file?Eroticism
A
1

@Audited annotation can be applied on Class, Method, and a Type. I had a similar issue and tried applying @Audited annotation on update... method and was able to see the audit information populated in the _AUD table.
Definition of @Audited is as below: https://docs.jboss.org/hibernate/orm/5.2/javadocs/org/hibernate/envers/Audited.html

Ambience answered 29/6, 2020 at 20:54 Comment(0)
I
0

You can manually clean modifed fields using @PostLoad annotation and then jpaRepository will update these columns (works for me with spring data 3.10).

Add following method to your audited entities or to abstract base audited entity:

    @PostLoad
    public void prepareModifiedFields() {
        this.modifiedBy = null;
        this.modifiedDate = null;
    }
Infantine answered 7/7, 2023 at 8:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.