@CreatedDate
private Date created;
@LastModifiedDate
private Date updated;
@CreatedBy
@ManyToOne
private Account createdBy;
@LastModifiedBy
@ManyToOne
private Account updatedBy;
@EnableJpaAuditing
추가@EnableJpaAuditing
@SpringBootApplication
public class Application {
@EntityListeners(AuditingEntityListener.class)
추가@Getter
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public class TimeStamp {
@CreatedDate
private LocalDateTime createdAt;
@CreatedBy
@ManyToOne
private User createdBy;
@LastModifiedDate
private LocalDateTime modifiedAt;
@LastModifiedBy
@ManyToOne
private User modifiedBy;
}
@Service
public class UserAuditorAware implements AuditorAware<User> {
@Override
public Optional<User> getCurrentAuditor() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication == null || !authentication.isAuthenticated()) {
return Optional.empty();
}
return Optional.of(((UserDetailsImpl) authentication.getPrincipal()).getUser());
}
}
@EnableJpaAuditing
에 AuditorAware 빈 이름 설정@EnableJpaAuditing(auditorAwareRef = "userAuditorAware") // auditorAware 의 빈이름을 넣어준다.
@SpringBootApplication
public class Application {
Insert
쿼리를 날릴 때 null인 값은 제외하고 쿼리문 만들어진다
@DynamicInsert
어노테이션 붙여주면 끝@DynamicInsert
public class User {
...
}
@Test
void dynamicInsertTest() {
// given
var newUser = User.builder().username("user").build();
// when
userRepository.save(newUser);
// then
// 부분 생성 쿼리
}
Update
쿼리를 날릴 때 null인 값은 제외하고 쿼리문 만들어진다
@DynamicUpdate
public class User {
...
}
@Test
void dynamicUpdateTest() {
// given
var newUser = User.builder().username("user").password("password").build();
userRepository.save(newUser);
// when
newUser.updatePassword("new password");
userRepository.save(newUser);
// then
// 부분 수정 쿼리
}