spring data jpa : auditing

This commit is contained in:
haerong22
2021-09-02 22:44:52 +09:00
parent cf66d50314
commit aace14b55d
6 changed files with 116 additions and 1 deletions

View File

@@ -2,7 +2,14 @@ package com.example.springdatajpa;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.data.domain.AuditorAware;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import java.util.Optional;
import java.util.UUID;
@EnableJpaAuditing
@SpringBootApplication @SpringBootApplication
public class SpringDataJpaApplication { public class SpringDataJpaApplication {
@@ -10,4 +17,10 @@ public class SpringDataJpaApplication {
SpringApplication.run(SpringDataJpaApplication.class, args); SpringApplication.run(SpringDataJpaApplication.class, args);
} }
@Bean
public AuditorAware<String> auditorProvider() {
// 세션에서 유저 아이디를 가져와서 사용한다.
// Todo
return () -> Optional.of(UUID.randomUUID().toString());
}
} }

View File

@@ -0,0 +1,23 @@
package com.example.springdatajpa.entity;
import lombok.Getter;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import javax.persistence.Column;
import javax.persistence.EntityListeners;
import javax.persistence.MappedSuperclass;
@EntityListeners(AuditingEntityListener.class)
@Getter
@MappedSuperclass
public class BaseEntity extends BaseTimeEntity{
@CreatedBy
@Column(updatable = false)
private String createdBy;
@LastModifiedBy
private String lastModifiedBy;
}

View File

@@ -0,0 +1,24 @@
package com.example.springdatajpa.entity;
import lombok.Getter;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import javax.persistence.Column;
import javax.persistence.EntityListeners;
import javax.persistence.MappedSuperclass;
import java.time.LocalDateTime;
@EntityListeners(AuditingEntityListener.class)
@Getter
@MappedSuperclass
public class BaseTimeEntity {
@CreatedDate
@Column(updatable = false)
private LocalDateTime createdDate;
@LastModifiedDate
private LocalDateTime lastModifiedDate;
}

View File

@@ -0,0 +1,30 @@
package com.example.springdatajpa.entity;
import lombok.Getter;
import javax.persistence.Column;
import javax.persistence.MappedSuperclass;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import java.time.LocalDateTime;
@Getter
@MappedSuperclass
public class JpaBaseEntity {
@Column(updatable = false)
private LocalDateTime createdDate;
private LocalDateTime updatedDate;
@PrePersist
public void prePersist() {
LocalDateTime now = LocalDateTime.now();
createdDate = now;
updatedDate = now;
}
@PreUpdate
public void preUpdate() {
updatedDate = LocalDateTime.now();
}
}

View File

@@ -13,7 +13,7 @@ import javax.persistence.*;
query="select m from Member m where m.username = :username" query="select m from Member m where m.username = :username"
) )
@NamedEntityGraph(name = "Member.all", attributeNodes = @NamedAttributeNode("team")) @NamedEntityGraph(name = "Member.all", attributeNodes = @NamedAttributeNode("team"))
public class Member { public class Member extends BaseEntity{
@Id @GeneratedValue @Id @GeneratedValue
@Column(name = "member_id") @Column(name = "member_id")

View File

@@ -1,6 +1,8 @@
package com.example.springdatajpa.entity; package com.example.springdatajpa.entity;
import com.example.springdatajpa.repository.MemberRepository;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.Rollback; import org.springframework.test.annotation.Rollback;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
@@ -20,6 +22,9 @@ class MemberTest {
@PersistenceContext @PersistenceContext
EntityManager em; EntityManager em;
@Autowired
MemberRepository memberRepository;
@Test @Test
void testEntity() { void testEntity() {
Team teamA = new Team("teamA"); Team teamA = new Team("teamA");
@@ -51,5 +56,25 @@ class MemberTest {
} }
} }
@Test
void JpaEventBaseEntity() throws Exception {
// given
Member member = new Member("member1");
memberRepository.save(member); // @PrePersist
Thread.sleep(100);
member.setUsername("member2");
em.flush();
em.clear();
// when
Member findMember = memberRepository.findById(member.getId()).get();
// then
System.out.println("findMember.getCreatedDate = " + findMember.getCreatedDate());
System.out.println("findMember.getLastModifiedDate = " + findMember.getLastModifiedDate());
System.out.println("findMember.getCreatedBy() = " + findMember.getCreatedBy());
System.out.println("findMember.getLastModifiedBy() = " + findMember.getLastModifiedBy());
}
} }