#26 pharmacy: jpa auditing

This commit is contained in:
haerong22
2022-12-14 22:51:02 +09:00
parent 6d1307f2ae
commit 8619e8d6e4
4 changed files with 51 additions and 1 deletions

View File

@@ -0,0 +1,24 @@
package com.example.road;
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;
@Getter
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public abstract class BaseTimeEntity {
@CreatedDate
@Column(updatable = false)
private LocalDateTime createdDate;
@LastModifiedDate
private LocalDateTime modifiedDate;
}

View File

@@ -2,7 +2,9 @@ package com.example.road;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
@EnableJpaAuditing
@SpringBootApplication
public class RoadApplication {

View File

@@ -1,5 +1,6 @@
package com.example.road.pharmacy.entity;
import com.example.road.BaseTimeEntity;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
@@ -15,7 +16,7 @@ import javax.persistence.Id;
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class Pharmacy {
public class Pharmacy extends BaseTimeEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)

View File

@@ -4,6 +4,8 @@ import com.example.road.AbstractIntegrationContainerBaseTest
import com.example.road.pharmacy.entity.Pharmacy
import org.springframework.beans.factory.annotation.Autowired
import java.time.LocalDateTime
class PharmacyRepositoryTest extends AbstractIntegrationContainerBaseTest {
@Autowired
@@ -59,4 +61,25 @@ class PharmacyRepositoryTest extends AbstractIntegrationContainerBaseTest {
result.size() == 1
}
def "BaseTimeEntity 등록"() {
given:
LocalDateTime now = LocalDateTime.now();
String address = "서울 특별시 성북구 종암동";
String name = "은혜 약국";
def pharmacy = Pharmacy.builder()
.pharmacyAddress(address)
.pharmacyName(name)
.build();
when:
pharmacyRepository.save(pharmacy);
def result = pharmacyRepository.findAll();
then:
result.get(0).getCreatedDate().isAfter(now);
result.get(0).getModifiedDate().isAfter(now);
}
}