๋ฒ ์˜ค
DCode
๋ฒ ์˜ค
์ „์ฒด ๋ฐฉ๋ฌธ์ž
์˜ค๋Š˜
์–ด์ œ
  • ๋ถ„๋ฅ˜ ์ „์ฒด๋ณด๊ธฐ (218)
    • ๊ณต์ง€์‚ฌํ•ญ (1)
    • ์žก์„ค (1)
    • Programming (33)
      • [C] (1)
      • [Java] (4)
      • [Python] (2)
      • [Android] (2)
      • [Network] (0)
      • [Operation System] (2)
      • [Spring Boot] (22)
      • [Docker] (0)
    • Algorithm (31)
      • ์ž๋ฃŒ๊ตฌ์กฐ (2)
      • ์•Œ๊ณ ๋ฆฌ์ฆ˜ (Java) (14)
      • ์•Œ๊ณ ๋ฆฌ์ฆ˜ (๊ธฐ์ดˆ) (15)
    • Coding Test (131)
      • BOJ (131)
      • Algospat (0)
    • ์ด๋ก ์ ์ธ๊ฑฐ (14)
      • ๋ณด์•ˆ (5)
      • ์˜ค๋ฅ˜ ํ•ด๊ฒฐ (2)
      • ๋””์ž์ธ ํŒจํ„ด (5)
      • ๋„คํŠธ์›Œํฌ (1)
      • ๊ธฐํƒ€ (1)
    • ์ตœ์‹ ๊ธฐ์ˆ  (4)
      • ๋ธ”๋ก์ฒด์ธ (1)
    • [Project] (1)

๋ธ”๋กœ๊ทธ ๋ฉ”๋‰ด

  • ๐Ÿˆโ€โฌ› GitHub
  • ๐Ÿ“ซ ๋ฐฉ๋ช…๋ก
  • ๐Ÿ”– ํƒœ๊ทธ

๊ณต์ง€์‚ฌํ•ญ

์ธ๊ธฐ ๊ธ€

ํ‹ฐ์Šคํ† ๋ฆฌ

hELLO ยท Designed By ์ •์ƒ์šฐ.
๋ฒ ์˜ค

DCode

Programming/[Spring Boot]

[Spring Data JPA] - JPA Auditing

2023. 3. 30. 18:26

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
    'Programming/[Spring Boot]' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
    • [Spring Data JPA] - ์ •๋ ฌ๊ณผ ํŽ˜์ด์ง• ์ฒ˜๋ฆฌ
    • Spring Data JPA ํ™œ์šฉํ•˜๊ธฐ
    • [Spring Data JPA] - ์ฟผ๋ฆฌ ๋ฉ”์„œ๋“œ์˜ ์ฃผ์ œ(Subject) ํ‚ค์›Œ๋“œ
    • [Spring Data JPA] - ์ฟผ๋ฆฌ ๋ฉ”์„œ๋“œ์˜ ์กฐ๊ฑด์ž ํ‚ค์›Œ๋“œ
    ๋ฒ ์˜ค
    ๋ฒ ์˜ค

    ํ‹ฐ์Šคํ† ๋ฆฌํˆด๋ฐ”