#26 pharmacy: jpa auditing
This commit is contained in:
24
road/src/main/java/com/example/road/BaseTimeEntity.java
Normal file
24
road/src/main/java/com/example/road/BaseTimeEntity.java
Normal 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;
|
||||||
|
}
|
||||||
@@ -2,7 +2,9 @@ package com.example.road;
|
|||||||
|
|
||||||
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.data.jpa.repository.config.EnableJpaAuditing;
|
||||||
|
|
||||||
|
@EnableJpaAuditing
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
public class RoadApplication {
|
public class RoadApplication {
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package com.example.road.pharmacy.entity;
|
package com.example.road.pharmacy.entity;
|
||||||
|
|
||||||
|
import com.example.road.BaseTimeEntity;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Builder;
|
import lombok.Builder;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
@@ -15,7 +16,7 @@ import javax.persistence.Id;
|
|||||||
@Builder
|
@Builder
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
public class Pharmacy {
|
public class Pharmacy extends BaseTimeEntity {
|
||||||
|
|
||||||
@Id
|
@Id
|
||||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
|
|||||||
@@ -4,6 +4,8 @@ import com.example.road.AbstractIntegrationContainerBaseTest
|
|||||||
import com.example.road.pharmacy.entity.Pharmacy
|
import com.example.road.pharmacy.entity.Pharmacy
|
||||||
import org.springframework.beans.factory.annotation.Autowired
|
import org.springframework.beans.factory.annotation.Autowired
|
||||||
|
|
||||||
|
import java.time.LocalDateTime
|
||||||
|
|
||||||
class PharmacyRepositoryTest extends AbstractIntegrationContainerBaseTest {
|
class PharmacyRepositoryTest extends AbstractIntegrationContainerBaseTest {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
@@ -59,4 +61,25 @@ class PharmacyRepositoryTest extends AbstractIntegrationContainerBaseTest {
|
|||||||
result.size() == 1
|
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);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user