springframework/Spring Data JPA

@CreationTimeStamp, @UpateTimestamp

Jungsoomin :) 2020. 12. 24. 14:51

하이버네이트 5.2 이상 부터 Entity 의 수정일과 등록일을 관리해주는 어노테이션을 제공한다.

 

Java8 에서 지원하는 시간 클래스를 사용할 수 있게 끔 해주고 있다.

 

@Entity
@Getter
@Setter
public class Reply {

    @Id @GeneratedValue
    private Long id;

    private String subject;

    @Column(unique = true)
    private Long accountId;

    private String author;

    private String content;
    @CreationTimestamp
    private LocalDateTime registerDate;
    @UpdateTimestamp
    private LocalDateTime updateDate;

    @ManyToOne(fetch = FetchType.EAGER)
    private Post post;

    public void mappingEntity(Post post) {
        this.post = post;
        post.getReplies().add(this);
    }

}

 

Duration 을 매핑하려고 하다가 찾았다.

 


자동으로 맵핑하는지 테스트 코드로 실험

@SpringBootTest
@TestPropertySource(locations = "classpath:test.properties")
class PostRepositoryTest {

    @Autowired
    private PostRepository postRepository;

    @Test
    public void repositoryLoading(){}

    @Test
    public void testRegister() {
        Post post = new Post();
        post.setAccountId(1L);
        post.setAuthor("SooMin");
        post.setContent("Post Content");
        post.setSubject("Post Subject");

        Post saved = postRepository.save(post);

        System.out.println(saved);
    }

}