๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Back-end ๋ฐ๋ธŒ์ฝ”์Šค/week 08 TIL (Jpa)

[TIL] 221206 - JPA : ์˜์†์„ฑ ์ปจํ…์ŠคํŠธ

by young-ji 2022. 12. 7.

JPA (2) - ์˜์†์„ฑ ์ปจํ…์ŠคํŠธ

  • EntityManagerFactory, EntityManager ํ•™์Šต
  • ์˜์†์„ฑ ์ปจํ…์ŠคํŠธ
  • ์—”ํ‹ฐํ‹ฐ์˜ ์ƒ๋ช…์ฃผ๊ธฐ
  • ์˜์†์„ฑ ์ปจํ…์ŠคํŠธ ํŠน์ง•๊ณผ ์‹ค์Šต

 

EntityManagerFactory, EntityManager

 

Entity

RDB์˜ table๊ณผ ๋งคํ•‘๋˜๋Š” ๊ฐ์ฒด

 

EntityManagerFactory

Entity๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” Entity Manager๋ฅผ ์ƒ์‚ฐํ•˜๋Š” ๊ณต์žฅ.

: ๋น„์šฉ์ด ๋น„์‹ธ ํ•˜๋‚˜๋งŒ ๋งŒ๋“ค์–ด์ง€๊ณ  Entity Manager๋ฅผ ์ƒ์‚ฐํ•˜๋Š” factory์„ฑ bean

: Thread safeํ•˜๋‹ค. ํ•˜๋‚˜์— ์—ฌ๋Ÿฌ ์Šค๋ ˆ๋“œ๊ฐ€ ์ ‘๊ทผํ•ด๋„ ์•ˆ์ „ํ•˜๋‹ค.

 

EntityManager

Entity๋ฅผ ์ €์žฅ, ์ˆ˜์ •, ์‚ญ์ œ, ์กฐํšŒ (CURD) ๋“ฑ entity์™€ ๊ด€๋ จ๋œ ๋ชจ๋“  ์ผ์„ ์ฒ˜๋ฆฌํ•œ๋‹ค.

: Thread Safeํ•˜์ง€ ์•Š๋‹ค. EntityManagerFactory์— ์˜ํ•ด ์ƒˆ๋กœ์šด manager๋ฅผ ์ƒ์„ฑํ•ด์ฃผ๋ฉฐ ๋‹จ์ผ ์Šค๋ ˆ๋“œ๊ฐ€ ์ ‘๊ทผํ•˜๋„๋ก ์‚ฌ์šฉํ•œ๋‹ค.

 

https://ttl-blog.tistory.com/108https://ttl-blog.tistory.com/108

์—”ํ‹ฐํ‹ฐ ๋ฉ”๋‹ˆ์ €๋Š” ํŠธ๋žœ์žญ์…˜์— commit์ด ์ผ์–ด๋‚ ๋•Œ ๊ทธ๋•Œ์„œ์•ผ connetion์„ ๊ฐ€์ ธ์™€ ์‚ฌ์šฉํ•œ๋‹ค.

 

 

์˜์†์„ฑ ์ปจํ…์ŠคํŠธ

 

์˜์†์„ฑ ์ปจํ…์ŠคํŠธ

  • JPA๋ฅผ ์ด์šฉํ•˜๋Š”๋ฐ ๊ฐ€์žฅ ์ค‘์š”ํ•œ ์š”์†Œ
  • Entity๋ฅผ ์˜๊ตฌ ์ €์žฅํ•˜๋Š” ํ™˜๊ฒฝ
  • Entity manager๋Š” ์—”ํ‹ฐํ‹ฐ๋ฅผ ์˜์†์„ฑ ์ปจํ…์ŠคํŠธ์— ๋ณด๊ด€ํ•˜๊ณ  ๊ด€๋ฆฌํ•œ๋‹ค.

 

์˜์†์„ฑ ์ปจํ…์ŠคํŠธ์˜ ํŠน์ง•

  • ์˜์†์„ฑ ์ปจํ…์ŠคํŠธ ์™€ ์‹๋ณ„์ž ๊ฐ’

: ์˜์†์„ฑ ์ปจํ…์ŠคํŠธ ์•ˆ์—์„œ ๊ด€๋ฆฌ๋˜๋Š” ์—”ํ‹ฐํ‹ฐ๋Š” ์‹๋ณ„์ž ๊ฐ’์„ ๋ฐ˜๋“œ์‹œ ๊ฐ€์ ธ์•ผํ•œ๋‹ค.

: key-value๋กœ ์—”ํ‹ฐํ‹ฐ๋ฅผ ๊ด€๋ฆฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— (1์ฐจ ์บ์‹œ์—์„œ)

@Entity class์— @id ํ•„๋“œ๋ฅผ ์ง€์ •ํ•˜์ง€ ์•Š์œผ๋ฉด ์˜ค๋ฅ˜๊ฐ€ ๋‚˜๋Š” ์ด์œ .

 

 

  • ์˜์†์„ฑ ์ปจํ…์ŠคํŠธ์™€ ๋ฐ์ดํ„ฐ ๋ฒ ์ด์Šค ์ €์žฅ

: JPA๋Š” ํŠธ๋žœ์žญ์…˜์„ ์ปค๋ฐ‹ํ•˜๋Š” ์ˆœ๊ฐ„ ์˜์†์„ฑ ์ปจํ…์ŠคํŠธ์— ์ƒˆ๋กœ ์ €์žฅ๋œ ์—”ํ‹ฐํ‹ฐ๋ฅผ DB์— ๋ฐ˜์˜ํ•œ๋‹ค.(FLUSH)

: ํ”Œ๋Ÿฌ์‹œ๋Š” ์˜์†์„ฑ ์ปจํ…์ŠคํŠธ์˜ ๋ณ€๊ฒฝ ๋‚ด์šฉ์„ DB์— ๋™๊ธฐํ™”ํ•˜๋Š” ์ž‘์—….

 

 

  • ์˜์†์„ฑ ์ปจํ…์ŠคํŠธ๊ฐ€ ์—”ํ‹ฐํ‹ฐ๋ฅผ ๊ด€๋ฆฌํ•จ์œผ๋กœ ์–ป๋Š” ์ด์ .
    1. 1์ฐจ ์บ์‹œ
    2. ๋™์ผ์„ฑ ๋ณด์žฅ
    3. ํŠธ๋žœ์žญ์…˜์„ ์ง€์›ํ•˜๋Š” ์“ฐ๊ธฐ ์ง€์—ฐ
    4. ๋ณ€๊ฒฝ ๊ฐ์ง€(dirty checking)
    5. ์ง€์—ฐ ๋กœ๋”ฉ

 

์—”ํ‹ฐํ‹ฐ ์ƒ๋ช…์ฃผ๊ธฐ

๋น„์˜์† (new / transient) : ์˜์†์„ฑ ์ปจํ…์ŠคํŠธ์™€ ์ „ํ˜€ ๊ด€๊ณ„๊ฐ€ ์—†๋Š” ์ƒํƒœ

// ๊ฐ์ฒด๊ฐ€ ์˜์†์„ฑ์ปจํ…์ŠคํŠธ, ๋ฐ์ดํŠธ๋ฒ ์ด์Šค์™€ ๋ฌด๊ด€ํ•œ ์ƒํƒœ์ด๋‹ค.
Customer customer = new Customer(); 
customer.setId(1L);
customer.setFirstName("honggu");
customer.setLastName("kang");

 

์˜์† (managed) : ์˜์†์„ฑ ์ปจํ…์ŠคํŠธ์— ์ €์žฅ๋œ ์ƒํƒœ

Customer customer = new Customer();
customer.setId(1L);
customer.setFirstName("honggu");
customer.setLastName("kang");

// customer๊ฐ์ฒด๊ฐ€ ์˜์†์„ฑ ์ปจํ…์ŠคํŠธ์—์„œ ๊ด€๋ฆฌ๋œ๋‹ค.
// em.find()๋‚˜ JPQL๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์กฐํšŒํ•œ ์—”ํ‹ฐํ‹ฐ๋„ ์˜์† ์ƒํƒœ.
em.persist(customer);

 

์ค€์˜์† (detached) : ์˜์†์„ฑ ์ปจํ…์ŠคํŠธ์— ์ €์žฅ๋˜์—ˆ๋‹ค๊ฐ€ ๋ถ„๋ฆฌ๋œ ์ƒํƒœ

// ์˜์†์ƒํƒœ์˜ customer๊ฐ์ฒด(์—”ํ‹ฐํ‹ฐ)๋ฅผ ์˜์†์„ฑ์ปจํ…์ŠคํŠธ์—์„œ ๋ถ„๋ฆฌํ•œ๋‹ค.
em.detach(customer);
// ์˜์†์ƒํƒœ์˜ ๋ชจ๋“  ๊ฐ์ฒด๋ฅผ ์˜์†์„ฑ์ปจํ…์ŠคํŠธ์—์„œ ๋ถ„๋ฆฌํ•œ๋‹ค.
em.clear()
// ์˜์†์„ฑ์ปจํ…์ŠคํŠธ๋ฅผ ์ข…๋ฃŒํ•œ๋‹ค.
em.close()

em.merge(customer);

 

์‚ญ์ œ (removed) : ์‚ญ์ œ๋œ ์ƒํƒœ

// customer ์—”ํ‹ฐํ‹ฐ๋ฅผ ์˜์†์„ฑ์ปจํ…์ŠคํŠธ์—์„œ ๋ถ„๋ฆฌํ•˜๊ณ , DB์—์„œ๋„ ์‚ญ์ œํ•œ๋‹ค.
em.remove(customer)

 

์˜์†์„ฑ ์ปจํ…์ŠคํŠธ๋ฅผ ํ”Œ๋Ÿฌ์‹œํ•˜๋Š” ๋ฐฉ๋ฒ• 3๊ฐ€์ง€

  1. em.flush() ์ง์ ‘ ํ˜ธ์ถœ.
  2. ํŠธ๋žœ์žญ์…˜ ์ปค๋ฐ‹์‹œ ํ”Œ๋Ÿฌ์‹œ๊ฐ€ ์ž๋™ ํ˜ธ์ถœ.
  3. JPQL (Java Persistence Query Langauge) ์ฟผ๋ฆฌ ์‹คํ–‰์‹œ ํ”Œ๋Ÿฌ์‹œ๊ฐ€ ์ž๋™ ํ˜ธ์ถœ.

 

 

 

์˜์†์„ฑ ์ปจํ…์ŠคํŠธ ์ดํ•ดํ•˜๊ธฐ

 

โœ… ์ €์žฅ

EntityManager em = emf.createEntityManager(); // 1)์—”ํ‹ฐํ‹ฐ ๋งค๋‹ˆ์ € ์ƒ์„ฑ

EntityTransaction transaction = em.getTransaction(); // 2)ํŠธ๋žœ์žญ์…˜ ํš๋“
transaction.begin(); // 3)ํŠธ๋ž™์žฐ์…• begin

Customer customer = new Customer(); // 4-1)๋น„์˜์†
customer.setId(1L);
customer.setFirstName("honggu");
customer.setLastName("kang");

em.persist(customer); // 4-2)์˜์†ํ™”

transaction.commit(); // 5)ํŠธ๋žœ์žญ์…˜ commit
// ํŠธ๋žœ์žญ์…˜์ด ์ปค๋ฐ‹์ด ๋˜๋Š” ์ˆœ๊ฐ„ ์ฟผ๋ฆฌ๊ฐ€ ์ˆ˜ํ–‰๋œ๋‹ค. flush DB์™€ ๋™๊ธฐํ™”๊ฐ€ ๋œ๋‹ค.

์“ฐ๊ธฐ ์ง€์—ฐ ์ €์žฅ์†Œ์— ์ฟผ๋ฆฌ ์ €์žฅ → 1์ฐจ ์บ์‹œ์— ์ €์žฅ → commit → DB์™€ ๋™๊ธฐํ™”

 

 

โœ… ์กฐํšŒ

→ 1์ฐจ ์บ์‹œ๋ฅผ ์ด์šฉํ•œ ์กฐํšŒ

@Test
void ์กฐํšŒ_1์ฐจ์บ์‹œ_์ด์šฉ() {
	EntityManager em = emf.createEntityManager();
  EntityTransaction transaction = em.getTransaction();
  transaction.begin();

  Customer customer = new Customer();
  customer.setId(1L);
  customer.setFirstName("honggu");
  customer.setLastName("kang");

	em.persist(customer);
  transaction.commit();

  Customer entity = em.find(Customer.class, 1L); // 1์ฐจ ์บ์‹œ์—์„œ ์กฐํšŒํ•œ๋‹ค.
  log.info("{} {}", entity.getFirstName(), entity.getLastName());
}

: 1์ฐจ ์บ์‹œ์— ์žˆ์„ ๊ฒฝ์šฐ, DB์— ์งˆ์˜ํ•˜์ง€ ์•Š๊ณ  1์ฐจ ์บ์‹œ์—์„œ ์ •๋ณด๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค.

 

→ DB๋ฅผ ์ด์šฉํ•œ ์กฐํšŒ

@Test
void ์กฐํšŒ() {
	EntityManager em = emf.createEntityManager();
	EntityTransaction transaction = em.getTransaction();
  transaction.begin();

	Customer customer = new Customer();
  customer.setId(2L);
  customer.setFirstName("guppy");
  customer.setLastName("hong");

  em.persist(customer);
  transaction.commit();

  em.clear(); //์˜์†์„ฑ ์ปจํ…์ŠคํŠธ๋ฅผ ์ดˆ๊ธฐํ™” ํ•œ๋‹ค.

  Customer entity = em.find(Customer.class, 2L); // DB ์—์„œ ์กฐํšŒํ•œ๋‹ค. SELECT ...
  log.info("{} {}", entity.getFirstName(), entity.getLastName());
  em.find(Customer.class, 2L); // SELECT Query ์ˆ˜ํ–‰๋˜์ง€ ์•Š๋Š”๋‹ค. 1์ฐจ์บ์‹œ ์‚ฌ์šฉ
}

: 1์ฐจ ์บ์‹œ์— ์—†์„ ๊ฒฝ์šฐ, DB์— ์งˆ์˜ํ•˜์—ฌ 1์ฐจ ์บ์‹œ์— ์ €์žฅํ›„ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜

 

 

โœ… ์ˆ˜์ •

@Test
void ์ˆ˜์ •() {
    EntityManager em = emf.createEntityManager();
    EntityTransaction transaction = em.getTransaction();
    transaction.begin();

    Customer customer = new Customer();
    customer.setId(1L);
    customer.setFirstName("honggu");
    customer.setLastName("kang");

    em.persist(customer);
    transaction.commit();
    // ์—”ํ‹ฐํ‹ฐ๋ฅผ ์˜์†ํ™”ํ•œํ›„, ์ปค๋ฐ‹์„ํ•ด์„œ flush()๋ฅผ ํ†ตํ•ด DB์— ์ €์žฅ.

    transaction.begin();

    Customer entity = em.find(Customer.class, 1L);
    entity.setFirstName("guppy");
    entity.setLastName("hong");

    // em.update(entity) ??!! 
    transacton.commit(); // flush -> UPDATE ... 
}

dirty checking(๋ณ€๊ฒฝ ๊ฐ์ง€) ์ˆ˜ํ–‰

→ ์Šค๋ƒ…์ƒท๊ณผ ๋น„๊ตํ•˜์—ฌ ๋ณ€๊ฒฝ์‚ฌํ•ญ์ด ์žˆ๋Š”์ง€ ํ™•์ธํ•œ๋‹ค. ๋ณ€๊ฒฝ ์‚ฌํ•ญ์ด ์žˆ์œผ๋ฉด ์“ฐ์ง€ ๊ธฐ์—ฐ ์ €์žฅ์†Œ์—์„œ update ์ฟผ๋ฆฌ๋ฅผ ๋‚ ๋ฆฐ๋‹ค.

: ์˜์†์„ฑ ์ปจํ…์ŠคํŠธ์— ์—”ํ‹ฐํ‹ฐ๋ฅผ ๋ณด๊ด€ํ• ๋•Œ, ์ตœ์ดˆ ์ƒํƒœ๋ฅผ ๋ณต์‚ฌํ•ด์„œ ์ €์žฅํ•ด๋‘๋Š”๋ฐ ์ด๊ฒƒ์„ ์Šค๋ƒ…์ƒท์ด๋ผ ํ•œ๋‹ค.

: ํ”Œ๋Ÿฌ์‹œ ์‹œ์ ์— ์Šค๋ƒ…์ƒท๊ณผ ์—”ํ‹ฐํ‹ฐ๋ฅผ ๋น„๊ตํ•˜์—ฌ ๋ณ€๊ฒฝ ์‚ฌํ•ญ์„ ์ฐพ์•„, ๋ณ€๊ฒฝ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค. ์ฆ‰, update๋ฅผ ์ง์ ‘ ํ•ด์ค„ ํ•„์š”๊ฐ€ ์—†๋‹ค! (์˜์† ์ƒํƒœ ์—”ํ‹ฐํ‹ฐ๋งŒ ์ ์šฉ๋จ)

: ๋ชจ๋“  ํ•„๋“œ๊ฐ€ ๋ณ€๊ฒฝ๋˜๋Š” ๊ฒƒ์ด ์‹ซ๋‹ค๋ฉด ์—”ํ‹ฐํ‹ฐ๋ฅผ ์ •์˜ํ•  ๋•Œ @DynamicUpdate ์–ด๋…ธํ…Œ์ด์…˜์„ ์ถ”๊ฐ€ํ•˜๋ฉด ์‹ค์ œ๋กœ update ๋˜๋Š” ํ•„๋“œ์— ๋Œ€ํ•ด์„œ๋งŒ update๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

 

 

โœ… ์‚ญ์ œ

@Test
void ์‚ญ์ œ() {
    EntityManager em = emf.createEntityManager();
    EntityTransaction transaction = em.getTransaction();
    transaction.begin();

    Customer customer = new Customer();
    customer.setId(1L);
    customer.setFirstName("honggu");
    customer.setLastName("kang");

    em.persist(customer);
    transaction.commit();

    transaction.begin();

    Customer entity = em.find(Customer.class, 1L);
    em.remove(entity);
    
    transaction.commit(); // flush -> DELETE ..
}

 

์˜์†์„ฑ ์ปจํƒ์ŠคํŠธ๋ฅผ ์ด์šฉํ•˜๋ฉด JpaRepository ์ธํ„ฐํŽ˜์ด์Šค๋Š” ํ•„์š”์—†๋‚˜?

 

์—”ํ‹ฐํ‹ฐ ๋งคํ•‘ - ๋‹จ์ผ ์—”ํ‹ฐํ‹ฐ ๋งคํ•‘

JPA ์—”ํ‹ฐํ‹ฐ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ• ๋•Œ, ๊ธฐ๋ณธ ์ƒ์„ฑ์ž๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

final ํ•„๋“œ๋Š” ๋ชป๋งŒ๋“œ๋‚˜?

 

 

๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์Šคํ‚ค๋งˆ ์ž๋™์ƒ์„ฑ

spring:
  h2:
    console:
      enabled: true
  jpa:
    generate-ddl: true
    database: H2
    show-sql: true
    open-in-view: false
    properties:
      hibernate:
        dialect: org.hibernate.dialect.H2Dialect
        query.in_clause_parameter_padding: true
        hbm2ddl:
          auto: create-drop  # ํ…Œ์ŠคํŠธ ์ฝ”๋“œ ์ž‘์„ฑํ• ๋•Œ ๋งŽ์ด ์‚ฌ์šฉ

์šด์˜ ํ™˜๊ฒฝ์—์„œ๋Š” validation, none ์„ ๋งŽ์ด ์‚ฌ์šฉํ•˜๊ณ 

create, create-drop, update ์„ค์ •์€ ์‹ค์ œ ํ…Œ์ด๋ธ”์— ์˜ํ–ฅ์„ ์ฃผ๊ธฐ ๋•Œ๋ฌธ์— ๊ฐœ๋ฐœํ™˜๊ฒฝ์ด๋‚˜ ํ…Œ์ŠคํŠธ ํ™˜๊ฒฝ์—์„œ ๋งŽ์ด ์‚ฌ์šฉํ•œ๋‹ค.

 

 

DDL ์˜ต์…˜

๊ตณ์ด ์‚ฌ์šฉํ•˜์ง€ ์•Š์•„๋„ ๊ฐ€๋…์„ฑ์„ ์œ„ํ•ด ๋ช…์‹œํ•ด ์ฃผ๋Š”๊ฒƒ์„ ๊ถŒ์žฅํ•œ๋‹ค.

@Entity
@Table(name = "member")
@Getter
@Setter
public class Member {
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    private Long id;

    @Column(name = "name", nullable = false, length = 30)
    private String name;

    @Column(nullable = false, length = 30, unique = true)
    private String nickName; // nick_name

    private int age; // age

    @Column(name = "addres", nullable = false) // ํฌ๊ธฐ๋ฅผ ์ •์˜ํ•˜์ง€์•Š์œผ๋ฉด ๋””ํดํŠธ ๊ฐ’ = 255
    private String address;
    
    @Column(name = "description", nullable = true)
    private String description;
}

—-

# drop ์˜ต์…˜
drop table if exists member CASCADE 
drop sequence if exists hibernate_sequence
# create ์˜ต์…˜
create sequence hibernate_sequence start with 1 increment by 1
create table member (id bigint not null, address varchar(255) not null, age integer not null, description varchar(255), name varchar(30) not null, nickName varchar(30) not null, primary key (id))
alter table member add constraint UK_1m3ighjll05v7njjxeopp823j unique (nickName)

 

 

๊ธฐ๋ณธํ‚ค ๋งคํ•‘์ „๋žต @Id

  • ์ง์ ‘ํ• ๋‹น
@Id
@Column(name = "id")
private Long id;

 

  • SEQUENCE

: ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์‹œํ€€์Šค์—์„œ ์‹๋ณ„์ž ๊ฐ’์„ ํš๋“ํ•œ ํ›„ ์˜์†ํ™”

: ORACLE, H2

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Long id;

 

  • TABLE

: ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์‹œํ€€์Šค ์ƒ์„ฑ์šฉ ํ…Œ์ด๋ธ”์—์„œ ์‹๋ณ„์ž ๊ฐ’์„ ํš๋“ํ•œ ํ›„ ์˜์†ํ™”

 

  • IDENTITY

: ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์—”ํ‹ฐํ‹ฐ๋ฅผ ์ €์žฅํ•ด์„œ ์‹๋ณ„์ž ๊ฐ’์„ ํš๋“ํ•œ ํ›„ ์˜์†ํ™”

: em.persist() ์‹œ์ ์— INSERT ์ฟผ๋ฆฌ๊ฐ€ ์ˆ˜ํ–‰๋˜๊ณ  ๊ทธ ๊ฒฐ๊ณผ๋กœ key ๊ฐ’์„ ๊ฐ€์ ธ์˜จ๋‹ค.

: MySQL (AUTO_INCREMENT)

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

 

  • AUTO

: ๋ฐ์ดํ„ฐ ๋ฒ ์ด์Šค ๋ฐฉ์–ธ(dialect)์— ๋”ฐ๋ผ์„œ ์ž๋™์œผ๋กœ ์ „๋žต์„ ์„ ํƒ. ์—”์ง„์— ๋งž๋Š” ์ตœ์ ํ™”๋œ ์ „๋žต์ด ์‚ฌ์šฉ๋œ๋‹ค.

 

 

๊ธฐํƒ€ ์ปฌ๋Ÿผ๋งคํ•‘

@Entity
@Table(name = "orders")
public class Order {

    @Id
    @Column(name = "id")
    private String uuid;

    @Column(name = "order_datetime", columnDefinition = "TIMESTAMP")
    private LocalDateTime orderDatetime;

    @Enumerated(EnumType.STRING)
    private OrderStatus orderStatus;

    @Lob // long text ํƒ€์ž…
    private String memo;
}

๋ฐ์ดํ„ฐ ์ค‘์‹ฌ ์„ค๊ณ„์˜ ๋ฌธ์ œ์ 

@Test
    void ์ž˜๋ชป๋œ_์„ค๊ณ„() {
        Member member = new Member();
        member.setName("youngji");
        member.setAddress("์šธ์ง‘");
        member.setAge(33);
        member.setNickName("young");
        member.setDescription("๋ฐฑ์•ค๋“œ ๊ฐœ๋ฐœ์ž์—์š”.");

        EntityManager entityManager = emf.createEntityManager();
        EntityTransaction transaction = entityManager.getTransaction();
        transaction.begin();

        entityManager.persist(member);
        Member memberEntity = entityManager.find(Member.class, 1L);

        Order order = new Order();
        order.setUuid(UUID.randomUUID().toString());
        order.setOrderDatetime(LocalDateTime.now());
        order.setOrderStatus(OPENED);
        order.setMemo("๋ถ€์žฌ์‹œ ์ „ํ™”์ฃผ์„ธ์š”.");
        order.setMemberId(memberEntity.getId()); // ์™ธ๋ž˜ํ‚ค๋ฅผ ์ง์ ‘ ์ง€์ •

        entityManager.persist(order);
        transaction.commit();

        Order orderEntity = entityManager.find(Order.class, order.getUuid());
        // FK ๋ฅผ ์ด์šฉํ•ด ํšŒ์› ๋‹ค์‹œ ์กฐํšŒ
        Member orderMemberEntity = entityManager.find(Member.class, orderEntity.getMemberId());
        // orderEntity.getMember() 
		// ๊ฐ์ฒด์ค‘์‹ฌ ์„ค๊ณ„๋ผ๋ฉด ๊ฐ์ฒด๊ทธ๋ž˜ํ”„ ํƒ์ƒ‰์„ ํ•ด์•ผํ•˜์ง€ ์•Š์„๊นŒ?
        log.info("nick : {}", orderMemberEntity.getNickName());
    }

order์—์„œ ํšŒ์› ๊ฐ์ฒด๋ฅผ ๊ฐ€์ ธ์˜ค๋Š”๊ฒŒ ์•„๋‹ˆ๋ผ FK๋กœ ๋‹ค์‹œ ํšŒ์›ํ…Œ์ด๋ธ”์„ ์กฐํšŒํ•ด์•ผํ•œ๋‹ค.

→ ์‹ค์ œ ์—”ํ‹ฐํ‹ฐ ๊ฐ์ฒด ์‚ฌ์ด์—๋Š” ์„œ๋กœ๋ฅผ ์ฐธ์กฐํ•˜์ง€ ์•Š๋Š”๋‹ค.

 

 

 

 

์ถœ์ฒ˜ - backend dev course ๊ฐ•ํ™๊ตฌ๋‹˜

๋Œ“๊ธ€