我正在开发一个关于Spring Data JPA和Postgres的示例。在这个示例中,我按照以下链接https://www.baeldung.com/database-auditing-jpa 和 Spring Boot JPA@CreatedDate @LastModifiedDate not being populated when saving the object实现了Auditing
。当我执行 repository.save 时,审计工作得非常好,在这种情况下,用@CreatedDate
和@LastModifiedDate
注释的两个字段都正确保存。
但是,当我尝试更新这个方法时,情况就不一样了。
我编写了以下方法:
@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);
}