๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Spring๐Ÿƒ

T ์•„์นด๋ฐ๋ฏธ : JPA ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๊ธฐ์ดˆ (1)

728x90

JPA ๊ธฐ์ดˆ ์ •๋ณต ํฌ์ธํŠธ

1. ๊ฐ์ฒด - RDB ๊ฐ„ ๋งคํ•‘(์„ค๊ณ„ ๊ณผ์ •)

2. ์‹ค์ œ JPA ๋™์ž‘ ~ ์˜์†์„ฑ ์ปจํ…Œ์ŠคํŠธ

 

๊ฐ์ฒด ๋งคํ•‘ํ•˜๊ธฐ

ํ•„์š”ํ•œ ์–ด๋…ธํ…Œ์ด์…˜ :

@Entity

@Id : DB PK์™€ ๋งคํ•‘ํ•  ํ•„๋“œ

 

Persistence.xml

JPA ์„ค์ • ํŒŒ์ผ

jakartax.persistence ~ : JPA ํ‘œ์ค€ ์†์„ฑ (๊ฑฐ์˜ ์‚ฌ์šฉ ์•ˆํ•จ)

hibernate : ํ•˜์ด๋ฒ„๋„ค์ดํŠธ ์ „์šฉ ์†์„ฑ

 

์‹ค์Šต : (* = ์Šคํ”„๋ง ๋ถ€ํŠธ๊ฐ€ ํ•˜๋Š” ์ผ ํŒŒ์•…ํ•˜๊ธฐ)

๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ผœ๊ธฐ

Maven์„ ์‚ฌ์šฉํ•œ ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ -> JDK 8, hibernate-entitymanager, h2database ์‚ฌ์šฉ

Main, ์—”ํ‹ฐํ‹ฐ ํŒจํ‚ค์ง€ - Member ํด๋ž˜์Šค ์ƒ์„ฑ + @Entity/@Id ~ ๊ธฐ๋ณธ DB ์—ฐ๊ฒฐ์„ธํŒ… ์™„๋ฃŒ

Getter, Setter ์„ค์ • ~ JPA ๊ตฌ๋™๋ฐฉ์‹

Persistence.xml ์„ค์ •

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence" version="2.2">
<persistence-unit name="hello">
<properties>
    <!-- ํ•„์ˆ˜ ์†์„ฑ -->
    <property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/>
    <property name="javax.persistence.jdbc.user" value="sa"/>
    <property name="javax.persistence.jdbc.password" value=""/>
    <property name="javax.persistence.jdbc.url" value="jdbc:h2:tcp://localhost/~/test"/>
    <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" />
    <!-- ์˜ต์…˜ -->
    <property name="hibernate.show_sql" value="true" />
    <property name="hibernate.format_sql" value="true" />
    <property name="hibernate.use_sql_comments" value="true" />
    <property name="hibernate.id.new_generator_mappings" value="true" />
    <property name="hibernate.hbm2ddl.auto" value="create" /> <!-- ์žˆ๋˜ table์„ dropํ•˜๊ณ  ์ƒˆ๋กœ ๋งŒ๋“ฌ -->
</properties>
</persistence-unit>
        </persistence>

 

๋ฐฉ์–ธ(Dialect) ์ง€์ • ๋ฐฉ๋ฒ• -> hibernate.dialect.๋ฐฉ์–ธ ~ ex) H2: org.hibernate.dialect.H2Dialect

 

์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๊ฐœ๋ฐœ

- ์—”ํ‹ฐํ‹ฐ ๋งค๋‹ˆ์ € ํŒฉํ† ๋ฆฌ ์„ค์ •

- ์—”ํ‹ฐํ‹ฐ ๋งค๋‹ˆ์ € ์„ค์ • : ์—”ํ‹ฐํ‹ฐ ๋งค๋‹ˆ์ € โ‰’ JPA

- ํŠธ๋žœ์ ์…˜

์ถœ์ฒ˜ : T ์•„์นด๋ฐ๋ฏธ, ํŠธ๋žœ์ ์…˜ ๋‹จ์œ„๋กœ ์—…๋ฌด๋ฅผ ์ฒ˜๋ฆฌํ•  ๋•Œ๋งˆ๋‹ค ์—”ํ‹ฐํ‹ฐ ๋งค๋‹ˆ์ €๋ฅผ ๋งŒ๋“ค์–ด์•ผ ํ•œ๋‹ค.

package hellojpa;

import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

public class Main {
    public static void main(String[] args) {
        EntityManagerFactory emf =
                Persistence.createEntityManagerFactory("hello"); 
                // persistence.xml ์„ค์ •์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์—”ํ‹ฐํ‹ฐ๋งค๋‹ˆ์ € ํŒฉํ† ๋ฆฌ ์ƒ์„ฑ
		
        EntityManager em = emf.createEntityManager(); // ์—”ํ‹ฐํ‹ฐ ๋งค๋‹ˆ์ € ์ƒ์„ฑ
        EntityTransaction tx = em.getTransaction(); // Transaction์„ ์–ป๋Š”๋‹ค
        tx.begin(); // ํŠธ๋žœ์ ์…˜ ์‹œ์ž‘
        
        try {
            Member member = new Member(); // ๊ฐ์ฒด ์ƒ์„ฑ ๋ฐ ๊ฐ’ ๋ณ€๊ฒฝ
            member.setId(1L);
            member.setName("hello");
            em.persist(member); // ์˜๊ตฌ์ €์žฅ
            tx.commit(); // ์ปค๋ฐ‹
        } catch (Exception e) {
            tx.rollback(); // ์˜ˆ์™ธ ๋ฐœ์ƒ์‹œ ํŠธ๋žœ์ ์…˜ ๋กค๋ฐฑ
        } finally {
            em.close(); // ์—”ํ‹ฐํ‹ฐ ๋งค๋‹ˆ์ €๋Š” ์‚ฌ์šฉ ํ›„ ๋ฌด์กฐ๊ฑด ๋‹ซ์•„์ค˜์•ผ ํ•œ๋‹ค.
        }
        emf.close();
    }
}

๋งค๋‹ˆ์ € ํ”„๋กœ์„ธ์Šค

ํŠธ๋žœ์ ์…˜์„ ์–ป๊ธฐ ~ JPA์˜ ๋ชจ๋“  ํ™œ๋™์€ Transaction ์•ˆ์—์„œ ์ด๋ฃจ์–ด์ง„๋‹ค -> ๊ฐ์ฒด ์ƒ์„ฑ ๋ฐ ์†์„ฑ ์ž‘์—… ์ˆ˜ํ–‰(๋™์ผ๋‚ด์šฉ ์ค‘๋ณต์ˆ˜ํ–‰์€ PK ๊ฐ’์˜ ์ถฉ๋Œ๋กœ ์˜ค๋ฅ˜๋ฐœ์ƒ) -> ๋งค๋‹ˆ์ €์— ๊ฐ์ฒด ๊ฐ’ ์ €์žฅ -> ์ปค๋ฐ‹ ํ›„ ์—”ํ‹ฐํ‹ฐ ๋งค๋‹ˆ์ € ์ข…๋ฃŒ

*์ฃผ์˜ํ•  ์  :

์—”ํ‹ฐํ‹ฐ ๋งค๋‹ˆ์ € ํŒฉํ† ๋ฆฌ๋Š” ํ•˜๋‚˜๋งŒ ์ƒ์„ฑํ•ด์„œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ „์ฒด์—์„œ ๊ณต์œ ํ•œ๋‹ค

์—”ํ‹ฐํ‹ฐ ๋งค๋‹ˆ์ €๋Š” ์“ฐ๋ ˆ๋“œ ๊ฐ„ ๊ณต์œ ํ•˜๋ฉด ์•ˆ๋œ๋‹ค(์‚ฌ์šฉํ•˜๊ณ  ๋ฒ„๋ ค์•ผ ํ•œ๋‹ค)

JPA์˜ ๋ชจ๋“  ๋ฐ์ดํ„ฐ ๋ณ€๊ฒฝ์€ ํŠธ๋žœ์ ์…˜ ์•ˆ์—์„œ ์‹คํ–‰

 

- ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง(CRUD)

 

JPA์˜ ์žฅ์  : ์ง์ ‘ ๋งคํ•‘์„ ํ•˜๊ธฐ ๋•Œ๋ฌธ์—, JPA๋ฅผ ํ†ตํ•ด ํ…Œ์ด๋ธ”์„ ์ง์ ‘ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.

~ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๋ฐฉ์–ธ์„ ํ™œ์šฉํ•ด ์ ์ ˆํ•œ DDL ์ƒ์„ฑ -> ์ด DDL์€ ๊ฐœ๋ฐœ ์žฅ๋น„์—์„œ๋งŒ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค(Create, Create-drop, Update)

์ถœ์ฒ˜ : T ์•„์นด๋ฐ๋ฏธ

๋งคํ•‘ ์–ด๋…ธํ…Œ์ด์…˜

@Column

name : ํ•„๋“œ์™€ ๋งคํ•‘ํ•  ํ…Œ์ด๋ธ”์˜ ์ปฌ๋Ÿผ ์ด๋ฆ„

insertable, updatable : ์ฝ๊ธฐ ์ „์šฉ

nullable : null ํ—ˆ์šฉ์—ฌ๋ถ€ ๊ฒฐ์ •, DDL ์ƒ์„ฑ ์‹œ ์‚ฌ์šฉ

unique : ์œ ๋‹ˆํฌ ์ œ์•ฝ ์กฐ๊ฑด, DDL ์ƒ์„ฑ ์‹œ ์‚ฌ์šฉ

columnDefinition, length, precision, scale (DDL)

@Temporal

@Enumerated

EnumType.String : ์—ด๊ฑฐํ˜• ์ด๋ฆ„์„ ๊ทธ๋Œ€๋กœ ์ €์žฅ

@Lob

CLOB, BLOB ๋งคํ•‘

CLOB : String, char[], java.sql.CLOB

BlOB : byte[], java.sql.BLOB

@Lob
private String lobString;
@Lob
private byte[] lobByte;

@Transient : DB์— ๋งคํ•‘ํ•˜๊ณ  ์‹ถ์ง€๋Š” ์•Š์€๋ฐ ๊ฐ์ฒด ์†์„ฑ์œผ๋กœ ์ €์žฅํ•ด๋‘๊ณ  ์‹ถ์€ ๊ฐ’

 

์‹๋ณ„์ž ๋งคํ•‘

@Id : ์ง์ ‘ ๋งคํ•‘

@GeneratedValue ~ @GeneratedValue(strategy = GenerationType.Auto) :

id ๊ฐ’์„ DB ์‹œํ€€์Šค ์˜ค๋ธŒ์ ํŠธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ž๋™์œผ๋กœ ๋ถ€์—ฌ

Auto ๋Œ€์‹  : Identity - MYSQL/ Sequence(@SequenceGenerator) - ORACLE/ Table(@TableGenerator) - ๋ชจ๋“  DB 

 

*์‹๋ณ„์ž ์ „๋žต

1) ๊ธฐ๋ณธ ํ‚ค ์ œ์•ฝ์กฐ๊ฑด : not null, ์œ ์ผํ•œ ๊ฐ’, ๋ณ€๊ฒฝ์ด ๋ถˆ๊ฐ€๋Šฅํ•ด์•ผ ํ•œ๋‹ค.

2) ๋น„์ฆˆ๋‹ˆ์Šค์™€ ๊ด€๊ณ„์—†๋Š” ๋Œ€๋ฆฌ ํ‚ค๋ฅผ ์‚ฌ์šฉํ•˜์ž ~ ๋ถˆ๋ณ€ํ•˜๋Š” ์ž์—ฐ ๊ฐ’์„ ์ฐพ๋Š” ๊ฒƒ์€ ์ •๋ง ์–ด๋ ต๋‹ค.

=> ๊ถŒ์žฅ : Long + ๋Œ€์ฒด ํ‚ค + ํ‚ค ์ƒ์„ฑ์ „๋žต

728x90