JPA Auditing
๐ก
๊ฐ ๋ฐ์ดํฐ๋ง๋ค ๋๊ฐ, ์ธ์ ์์ฑํ๊ณ ๋ณ๊ฒฝํ๋์ง ์๊ธฐ ์ํ ๋ฐฉ๋ฒ
๋ํ์ ์ผ๋ก ์ฌ์ฉ๋๋ ํ๋
- ์์ฑ ์ฃผ์ฒด
- ์์ฑ ์ผ์
- ๋ณ๊ฒฝ ์ฃผ์ฒด
- ๋ณ๊ฒฝ ์ผ์
๊ธฐ๋ฅ ์ฌ์ฉํด๋ณด๊ธฐ
JPA Auditing ๊ธฐ๋ฅ ํ์ฑํ
main ๋ฉ์๋๊ฐ ์๋ ํด๋์ค์์ @EnableJpaAuditing Annotation์ ์ถ๊ฐํ๋ค.
package com.springboot.advanced_jpa;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
@SpringBootApplication
@EnableJpaAuditing
public class AdvancedJpaApplication {
public static void main(String[] args) {
SpringApplication.run(AdvancedJpaApplication.class, args);
}
}
BaseEntity ์์ฑ
package com.springboot.advanced_jpa.data.entity;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
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
@Setter
@ToString
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public class BaseEntity {
@CreatedDate
@Column(updatable = false)
private LocalDateTime createdAt;
@LastModifiedDate
private LocalDateTime updatedAt;
}
Product Entity ๊ฐ์ฒด๋ฅผ ์์ ํ๋ค.
BaseEntity ํด๋์ค๋ฅผ ์์๋ฐ๋๋ค.
createdAt, updatedAt ํ๋๋ฅผ ์ ๊ฑฐํ๋ค.
package com.springboot.advanced_jpa.data.entity;
import lombok.*;
import javax.persistence.*;
@Entity
@Builder
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = true)
@ToString(exclude = "name")
@Table(name="product")
public class Product extends BaseEntity{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long number;
@Column(nullable = false)
private String name;
@Column(nullable = false)
private Integer price;
@Column(nullable=false)
private Integer stock;
}
ํ ์คํธํด๋ณด๊ธฐ
ํ ์คํธ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ์ด ์์ฑํ๋ค
package com.springboot.advanced_jpa.data.repository;
import com.springboot.advanced_jpa.data.entity.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
public class ProductRepositoryTest {
@Autowired
ProductRepository productRepository;
@Test
public void auditingTest(){
Product product = new Product();
product.setName("ํ");
product.setPrice(1000);
product.setStock(100);
Product savedProduct = productRepository.save(product);
System.out.println("productName : "+savedProduct.getName());
System.out.println("createdAt : "+savedProduct.getCreatedAt());
}
}
createdAt ์ด ์๋์ผ๋ก ์์ฑ๋ ๊ฒ์ ์ ์ ์๋ค.
Hibernate:
insert
into
product
(created_at, updated_at, name, price, stock)
values
(?, ?, ?, ?, ?)
productName : ํ
createdAt : 2023-03-30T18:13:49.256258500
Uploaded by N2T
'Programming > [Spring Boot]' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Spring Data JPA] - ์ ๋ ฌ๊ณผ ํ์ด์ง ์ฒ๋ฆฌ (0) | 2023.03.30 |
---|---|
Spring Data JPA ํ์ฉํ๊ธฐ (0) | 2023.03.29 |
[Spring Data JPA] - ์ฟผ๋ฆฌ ๋ฉ์๋์ ์ฃผ์ (Subject) ํค์๋ (0) | 2023.03.29 |
[Spring Data JPA] - ์ฟผ๋ฆฌ ๋ฉ์๋์ ์กฐ๊ฑด์ ํค์๋ (0) | 2023.03.29 |
ํ ์คํธ ์ฃผ๋ ๊ฐ๋ฐ(TDD) (0) | 2023.03.16 |