Skip to content

Commit

Permalink
CreatedDate, UpdatedDate final로 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
ddaaac committed Jul 15, 2020
1 parent 948fe3c commit b13ff8f
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 5 deletions.
39 changes: 39 additions & 0 deletions src/main/java/spring/data/jdbc/example/auditable/Issue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package spring.data.jdbc.example.auditable;

import java.time.LocalDateTime;

import javax.validation.constraints.NotBlank;

import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jdbc.core.mapping.AggregateReference;

import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.With;
import spring.data.jdbc.example.id.Member;

@Builder
@Getter
public class Issue {
@Id
@With(AccessLevel.PACKAGE)
private final Long id;

@CreatedBy
private final AggregateReference<Member, Long> memberId;

@NotBlank
private final String title;

@CreatedDate
@With(AccessLevel.PACKAGE)
private final LocalDateTime createdAt;

@LastModifiedDate
@With(AccessLevel.PACKAGE)
private final LocalDateTime updatedAt;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package spring.data.jdbc.example.auditable;

import org.springframework.data.repository.CrudRepository;

public interface IssueRepository extends CrudRepository<Issue, Long> {
}
5 changes: 5 additions & 0 deletions src/main/java/spring/data/jdbc/example/id/Orders.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@

import java.util.UUID;

import org.springframework.core.annotation.Order;
import org.springframework.data.annotation.Id;
import org.springframework.data.relational.core.conversion.MutableAggregateChange;
import org.springframework.data.relational.core.mapping.event.AbstractRelationalEventListener;
import org.springframework.data.relational.core.mapping.event.AfterSaveEvent;
import org.springframework.data.relational.core.mapping.event.BeforeSaveCallback;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

@AllArgsConstructor
@Getter
Expand All @@ -17,6 +21,7 @@ public class Orders {

private String title;

@Order(1)
public static class BeforeSaveOrderCallback implements BeforeSaveCallback<Orders> {
@Override
public Orders onBeforeSave(Orders aggregate, MutableAggregateChange<Orders> aggregateChange) {
Expand Down
20 changes: 15 additions & 5 deletions src/main/resources/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,10 @@ create table if not exists product

create table if not exists member
(
id bigint primary key,
name varchar(50),
password varchar(255)
id bigint primary key,
name varchar(50),
password varchar(255),
created_at datetime
);

create table if not exists subway
Expand Down Expand Up @@ -114,6 +115,15 @@ create table if not exists validation_entity

create table if not exists validation_child_entity
(
email varchar(255),
validation_entity bigint
email varchar(255),
validation_entity bigint
);

create table if not exists issue
(
id bigint primary key auto_increment,
member_id bigint,
title varchar(255),
created_at datetime,
updated_at datetime
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package spring.data.jdbc.example.auditable;

import static org.assertj.core.api.Assertions.*;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.jdbc.core.mapping.AggregateReference;

@SpringBootTest
class IssueRepositoryTest {
@Autowired
private IssueRepository issueRepository;

@Test
void name() {
final Issue issue = issueRepository.save(
Issue.builder().title("이슈").memberId(AggregateReference.to(1L)).build());

assertThat(issue.getCreatedAt()).isNotNull();
assertThat(issue.getUpdatedAt()).isNotNull();
}
}

0 comments on commit b13ff8f

Please sign in to comment.