๐ŸŒป JAVA/์ž๋ฐ” ORM ํ‘œ์ค€ JPA ํ”„๋กœ๊ทธ๋ž˜๋ฐ

7-2) ๊ณ ๊ธ‰ ๋งคํ•‘ : Mapped Superclass ๊ณตํ†ต ๋งคํ•‘ ์ •๋ณด ์ƒ์†

iseunghan 2020. 11. 5. 17:48
๋ฐ˜์‘ํ˜•

 

์ž๋ฐ” ORM ํ‘œ์ค€ JPA ํ”„๋กœ๊ทธ๋ž˜๋ฐ - ๊ธฐ๋ณธํŽธ์„ ๊ณต๋ถ€ํ•˜๋ฉฐ ์ •๋ฆฌํ•œ ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค.

 

์ž๋ฐ” ORM ํ‘œ์ค€ JPA ํ”„๋กœ๊ทธ๋ž˜๋ฐ - ๊ธฐ๋ณธํŽธ - ์ธํ”„๋Ÿฐ

JPA๋ฅผ ์ฒ˜์Œ ์ ‘ํ•˜๊ฑฐ๋‚˜, ์‹ค๋ฌด์—์„œ JPA๋ฅผ ์‚ฌ์šฉํ•˜์ง€๋งŒ ๊ธฐ๋ณธ ์ด๋ก ์ด ๋ถ€์กฑํ•˜์‹  ๋ถ„๋“ค์ด JPA์˜ ๊ธฐ๋ณธ ์ด๋ก ์„ ํƒ„ํƒ„ํ•˜๊ฒŒ ํ•™์Šตํ•ด์„œ ์ดˆ๋ณด์ž๋„ ์‹ค๋ฌด์—์„œ ์ž์‹ ์žˆ๊ฒŒ JPA๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ดˆ๊ธ‰ ์›น ๊ฐœ๋ฐœ ํ”„๋กœ๊ทธ๏ฟฝ

www.inflearn.com

 

 

 

๐Ÿš€ @MappedSuperclass

  • ๊ณตํ†ต ๋งคํ•‘ ์ •๋ณด๊ฐ€ ํ•„์š”ํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค!

  • ์ƒ์†๊ด€๊ณ„ ๋งคํ•‘ X 
  • ์—”ํ‹ฐํ‹ฐ X, ํ…Œ์ด๋ธ”๊ณผ ๋งคํ•‘ X
  • ๋ถ€๋ชจ ํด๋ž˜์Šค๋ฅผ ์ƒ์† ๋ฐ›๋Š” ์ž์‹ ํด๋ž˜์Šค์— ๋งคํ•‘ ์ •๋ณด๋งŒ ์ œ๊ณตํ•œ๋‹ค.
  • ์กฐํšŒ, ๊ฒ€์ƒ‰ ๋ถˆ๊ฐ€ (em.find(BaseEntity) ๋ถˆ๊ฐ€!)
  • ์ง์ ‘ ์ƒ์„ฑํ•ด์„œ ์‚ฌ์šฉํ•  ์ผ์ด ์—†์œผ๋ฏ€๋กœ ์ถ”์ƒ (abstract) ํด๋ž˜์Šค ๊ถŒ์žฅ

 

import javax.persistence.MappedSuperclass;
import java.time.LocalDateTime;

/**
 * @MappedSuperclass : ๊ณตํ†ต์˜ ๋งคํ•‘ ์ •๋ณด๊ฐ€ ํ•„์š”ํ•  ๋•Œ
 * ๋‚ด๊ฐ€ ์›ํ•˜๋Š” ํ…Œ์ด๋ธ”์— ๋ˆ„๊ฐ€ ์ƒ์„ฑํ–ˆ๊ณ (createdBy), ์–ธ์ œ ์ƒ์„ฑํ–ˆ๋Š”์ง€(createdByDate), ๋“ฑ๋“ฑ
 * ์ถ”๊ฐ€ ํ•˜๊ณ  ์‹ถ์„๋•Œ, ๊ท€์ฐฎ์Œ์„ ๋œ์–ด์ฃผ๋Š” ์• ๋…ธํ…Œ์ด์…˜์ด๋‹ค.
 *
 * (*) ์ด ํด๋ž˜์Šค๋Š” ์ƒ์„ฑํ•ด์„œ ์‚ฌ์šฉํ•  ์ผ์ด ์—†์œผ๋ฏ€๋กœ ์ถ”์ƒํด๋ž˜์Šค๋กœ ์„ ์–ธ!!
 *
 * ๋ฐฉ๋ฒ•. BaseEntity๋ผ๋Š” ํด๋ž˜์Šค๋ฅผ ์ƒ์„ฑํ•ด์„œ ์ถ”๊ฐ€ํ•˜๊ณ  ์‹ถ์€ ์ •๋ณด๋“ค์„ ๋„ฃ๊ณ , @MappedSuperclass ๋งŒ ๋ถ™์—ฌ์ฃผ๋ฉด ๋!
 * ๊ทธ๋ฆฌ๊ณ  ์ถ”๊ฐ€ ํ•˜๊ณ  ์‹ถ์€ ์—”ํ‹ฐํ‹ฐ์— ์ƒ์†์„ ์‹œ์ผœ์ฃผ๋ฉด ๋œ๋‹ค.
 * ์˜ˆ) Book ์— ์ถ”๊ฐ€ํ•˜๊ณ ์‹ถ๋‹ค -> public class Book extends BaseEntity ..
 */
@MappedSuperclass
public abstract class BaseEntity {

    private String createdBy;
    private LocalDateTime createdByDate;
    private String lastModifiedBy;
    private LocalDateTime lastModifiedDate;

    public String getCreatedBy() {
        return createdBy;
    }

    public void setCreatedBy(String createdBy) {
        this.createdBy = createdBy;
    }

    public LocalDateTime getCreatedByDate() {
        return createdByDate;
    }

    public void setCreatedByDate(LocalDateTime createdByDate) {
        this.createdByDate = createdByDate;
    }

    public String getLastModifiedBy() {
        return lastModifiedBy;
    }

    public void setLastModifiedBy(String lastModifiedBy) {
        this.lastModifiedBy = lastModifiedBy;
    }

    public LocalDateTime getLastModifiedDate() {
        return lastModifiedDate;
    }

    public void setLastModifiedDate(LocalDateTime lastModifiedDate) {
        this.lastModifiedDate = lastModifiedDate;
    }
}

 

@Entity
public abstract class Item extends BaseEntity {
	@Id @GeneratedValue
    private Long id;
    
    private String name;
    private int price;
    
    // getter, setter ์ƒ๋žต
}

 

public class JpaMain {
	public static void main(String[] args) {
		EntityManagerFactory emf = Persistence.createEntityManagerFactory("chapter7"); //persistenceName : resource/META-INF/persistence.xml ํŒŒ์ผ์— ์„ค์ •ํ•œ name
        EntityManager em = emf.createEntityManager();
        // code
        EntityTransaction tx = em.getTransaction();
        tx.begin();

        try { 
        	Book book = new Book();
            book.setName("book1");
            book.setCreatedBy("set1");
            book.setCreatedByDate(LocalDateTime.now());

            em.persist(book);

            em.flush();
            em.clear();

            Book findBook = em.find(Book.class, book.getId());
            String createdBy = findBook.getCreatedBy();
            LocalDateTime createdByDate = findBook.getCreatedByDate();
            System.out.println(createdBy + ", " + createdByDate);

            tx.commit(); // ์ด๋•Œ ์Œ“์•„๋’€๋˜ ์ฟผ๋ฆฌ๋ฅผ ํ•œ๋ฐฉ์— ๋‚ ๋ฆฐ๋‹ค.
         } catch (Exception e) {
            tx.rollback();
         } finally {
            em.close();
         }
         
         emf.close(); // ํ•ญ์ƒ ๊ผญ ๋งˆ์ง€๋ง‰์— ๋‹ซ์•„์ค˜์•ผํ•จ!
	}
}

์ด๋ ‡๊ฒŒ ๋˜๋ฉด Item์„ ํ™•์žฅํ•˜๊ณ  ์žˆ๋Š” Book ํด๋ž˜์Šค์—๋„ ๊ณตํ†ต ๋งคํ•‘ ์ •๋ณด๊ฐ€ ์ƒ๊ธฐ๊ฒŒ ๋œ๋‹ค.

๊ทธ๋ž˜์„œ book.setCreateBy() ๋ฅผ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค.

๋ฐ˜์‘ํ˜•