[JPA] 2.JPA ์‹œ์ž‘ํ•˜๊ธฐ

 

1. Hello JPA - ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ

ํ•ด๋‹น ๊ฐ•์˜์—์„œ๋Š” Maven๊ณผ H2 ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์‚ฌ์šฉ

์ž๋ฐ” 8 ์ด์ƒ(8 ๊ถŒ์žฅ)

๋ฉ”์ด๋ธ ์„ค์ •

groupId:jpa-basic
artifactId: ex1-hello-jpa
version: 1.0.0

dependencies - pom.xml

 <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
    <groupId>jpa-basic</groupId>
    <artifactId>ex1-hello-jpa</artifactId>
    <version>1.0.0</version>

  <dependencies>
    <!-- JPA ํ•˜์ด๋ฒ„๋„ค์ดํŠธ -->
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-entitymanager</artifactId>
      <version>5.3.10.Final</version>
    </dependency>
    
    <!-- H2 ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค -->
    <dependency>
      <groupId>com.h2database</groupId>
      <artifactId>h2</artifactId>
      <version>1.4.199</version>
    </dependency>
  </dependencies>
</project>

- hibernate : ํ•˜์ด๋ฒ„๋„ค์ดํŠธ ์ฝ”์–ด

 

JPA ์„ค์ • - persistence.xml

JPA ์„ค์ • ํŒŒ์ผ
/META-INF/persistence.xml ์œ„์น˜

persistence-unit name์œผ๋กœ ์ด๋ฆ„ ์ง€์ •
avax.persistence๋กœ ์‹œ์ž‘: JPA ํ‘œ์ค€ ์†์„ฑ
hibernate๋กœ ์‹œ์ž‘: ํ•˜์ด๋ฒ„๋„ค์ดํŠธ ์ „์šฉ ์†์„ฑ
<?xml version="1.0" encoding="UTF-8"?>
  <persistence version="2.2"
  xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd">
  <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.hbm2ddl.auto" value="create" />-->
    </properties>
  </persistence-unit>
</persistence>

** javax ์‹œ์ž‘ : hibernate ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋ง๊ณ  ๋‹ค๋ฅธ JPA ๊ตฌํ˜„ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์จ๋„ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ์Œ (ํ‘œ์ค€ ์ง€ํ‚ค๋Š” ๊ฒƒ)

** hibernate ์‹œ์ž‘ : hibernate ์ „์šฉ ์˜ต์…˜

 

** ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๋ฐฉ์–ธ

- JPA๋Š” ํŠน์ • ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ข…์† X
- ๊ฐ๊ฐ์˜ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๊ฐ€ ์ œ๊ณตํ•˜๋Š” SQL ๋ฌธ๋ฒ•๊ณผ ํ•จ์ˆ˜๋Š” ์กฐ๊ธˆ์”ฉ ๋‹ค๋ฆ„
> ๊ฐ€๋ณ€ ๋ฌธ์ž : MySQL์€ VARCHAR, Oracle์€ VARCHAR2
> ๋ฌธ์ž์—ด์„ ์ž๋ฅด๋Š” ํ•จ์ˆ˜ : SQL ํ‘œ์ค€์€ SUBSTRING(), Oracle์€ SUBSTR()
> ํŽ˜์ด์ง• : MySQL์€ LIMIT, Oracle์€ ROWNUM

- ๋ฐฉ์–ธ : SQL ํ‘œ์ค€์„ ์ง€ํ‚ค์ง€ ์•Š๋Š” ํŠน์ • ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋งŒ์˜ ๊ณ ์œ ํ•œ ๊ธฐ๋Šฅ

hibernate.dialect ์†์„ฑ์— ์ง€์ •
  > H2 : org.hibernate.dialect.H2Dialect
  > Oracle 10g : org.hibernate.dialect.Oracle10gDialect
  > MySQL : org.hibernate.dialect.MySQL5InnoDBDialect

ํ•˜์ด๋ฒ„๋„ค์ดํŠธ๋Š” 40๊ฐ€์ง€ ์ด์ƒ์˜ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๋ฐฉ์–ธ ์ง€์›

 


2. Hello JPA - ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๊ฐœ๋ฐœ

JPA ๊ตฌ๋™ ๋ฐฉ์‹

- ํ•„์š”ํ• ๋•Œ๋งˆ๋‹ค EntityManager ์ฐ์–ด๋ƒ„

 

 

JpaMain.class

ใ„ดjava.hellojpa์•„๋ž˜

public class JpaMain {
  public static void main(String[] args){
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("hello")
    EntityManager em = emf.createEntityManager();
    
    // code
    em.close();
    emf.close();
  }
}

 

๊ฐ์ฒด์™€ ํ…Œ์ด๋ธ”์„ ์ƒ์„ฑํ•˜๊ณ  ๋งตํ•‘ํ•˜๊ธฐ

- @Entity : JPA๊ฐ€ ๊ด€๋ฆฌํ•  ๊ฐ์ฒด

- @Id : ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค PK์™€ ๋งตํ•‘

 

 @Entity
 public class Member {
   @Id
   private Long id;
   private String name;
  //Getter, Setter ...
}
create table Member (
 id bigint not null,
 name varchar(255),
 primary key(id)
);

 

์‹ค์Šต - ํšŒ์›์ €์žฅ

- ํšŒ์›๋“ฑ๋ก

- ํšŒ์›์ˆ˜์ •

- ํšŒ์›์‚ญ์ œ

- ํšŒ์› ๋‹จ ๊ฑด ์กฐํšŒ

 

[๋“ฑ๋ก]

JpaMain

public class JpaMain {
  public static void main(String[] args){
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("hello")
    EntityManager em = emf.createEntityManager();
    
    // connection ๋ฐ›๋Š” ๊ฒƒ์ฒ˜๋Ÿผ transaction ์ด ์ค‘์š”
    Entitytransaction tx = em.getTransaction();
    tx.begin();
    
    try{
      Member member = new Member();
      member.setId(1L);
      member.setName("helloA");
    
      em.persist(member);
    
      tx.commit();
    } catch (Exception e) {
      tx.rollback();
    } finally {
      em.close();
    }
    
    emf.close();
  }
}

 

Hibernate ์ฃผ์„

Hibernate:
  /* insert hellojpa.Member
      */  insert
      into
           Member
           (name, id)
      values
            (?,?)

-> persistence.xml์— ๋„ฃ์€ ์ฃผ์„ ์˜ต์…˜ ๋•Œ๋ฌธ!

 

**@Table, @Column

@Table(name = "USER")์ด๋ผ๊ณ ํ•˜๋ฉด userํ…Œ์ด๋ธ”๋กœ ๋“ค์–ด๊ฐ [public class Member ์œ„์—]

@Column(name = "username")์ด๋ผ๊ณ ํ•˜๋ฉด username ์นผ๋Ÿผ์— ๋“ค์–ด๊ฐ [private String name ์œ„์—]

 

[์ˆ˜์ •]

public class JpaMain {
  public static void main(String[] args){
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("hello")
    EntityManager em = emf.createEntityManager();
    
    // connection ๋ฐ›๋Š” ๊ฒƒ์ฒ˜๋Ÿผ transaction ์ด ์ค‘์š”
    Entitytransaction tx = em.getTransaction();
    tx.begin();
    
    try{
      Member findmember = em.find(Member.class, 1L);
      findmember.setName("HelloJ");
    
      tx.commit();
    } catch (Exception e) {
      tx.rollback();
    } finally {
      em.close();
    }
    
    emf.close();
  }
}

- JPA ํ†ตํ•ด์„œ entity๊ฐ€์ ธ์˜ค๋ฉด JPA๊ฐ€ ๊ด€๋ฆฌํ•˜๊ณ  ๋ณ€๊ฒฝ์ด ๋˜์—ˆ๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ transaction commitํ•˜๋Š” ์‹œ์ ์— ์ฒดํฌํ•ด์„œ ์ž๋™์œผ๋กœ update ์ฟผ๋ฆฌ๋ฅผ ๋‚ ๋ฆผ

 

 

์ฃผ์˜)

- emf๋Š” ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์„œ๋น„์Šค๋ฅผ ํ•œ๋‹ค๋ฉด ์›น์„œ๋ฒ„๊ฐ€ ์˜ฌ๋ผ์˜ค๋Š” ์‹œ์ ์— db๋‹น ํ•˜๋‚˜๋งŒ ์ƒ์„ฑ

- em๋Š” ๊ณ ๊ฐ ์š”์ฒญ ์˜ฌ๋•Œ๋งˆ๋‹ค ์‚ฌ์šฉํ•˜๊ณ  ๋ฒ„๋ฆผ

- em์€ thread ๊ฐ„ ๊ณต์œ ํ•˜๋ฉด ์•ˆ๋จ! (์‚ฌ์šฉํ•˜๊ณ  ๋ฒ„๋ ค์•ผํ•จ) -> db connection ์“ฐ๊ณ  ๋Œ๋ ค์ฃผ๋Š” ๊ฒƒ์ฒ˜๋Ÿผ

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


3. JPQL ์†Œ๊ฐœ

- ๊ฐ€์žฅ ๋‹จ์ˆœํ•œ ์กฐํšŒ ๋ฐฉ๋ฒ• -> em.find(), ๊ฐ์ฒด ๊ทธ๋ž˜ํ”„ ํƒ์ƒ‰( a.getB().getC() )

- ๋‚˜์ด๊ฐ€ 18์‚ด ์ด์ƒ์ธ ํšŒ์›์„ ๋ชจ๋‘ ๊ฒ€์ƒ‰ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด?

 

 

๋ฌธ์ œ) ๊ฒ€์ƒ‰ ์ฟผ๋ฆฌ

- ๊ฒ€์ƒ‰์„ ํ• ๋•Œ๋„ ํ…Œ์ด๋ธ”์ด ์•„๋‹Œ ์—”ํ‹ฐํ‹ฐ ๊ฐ์ฒด๋ฅผ ๋Œ€์ƒ์œผ๋กœ ๊ฒ€์ƒ‰

- ๋ชจ๋“  DB ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ์ฒด๋กœ ๋ณ€ํ™˜ํ•ด์„œ ๊ฒ€์ƒ‰ํ•˜๋Š” ๊ฒƒ์€ ๋ถˆ๊ฐ€๋Šฅ

- ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ๋งŒ DB์—์„œ ๋ถˆ๋Ÿฌ์˜ค๋ ค๋ฉด ๊ฒฐ๊ตญ ๊ฒ€์ƒ‰ ์กฐ๊ฑด์ด ํฌํ•จ๋œ SQL์ด ํ•„์š”

- ์—”ํ‹ฐํ‹ฐ ๊ฐ์ฒด๋ฅผ ์ค‘์‹ฌ์œผ๋กœ ๊ฐœ๋ฐœ

 

->List<Member> result = 

em.createQuery("select m from Member as m where m.name = ", Member.class).getResultList();

 

+ getResult ์ „์— .setFirstresult(5), .setMaxResults(8) = rownum ์ˆœ์œ„๊ฐ’ ๊ฐ€๋Šฅ

 

- SQL ์ถ”์ƒํ™”ํ•œ JPQL์ด๋ผ๋Š” ๊ฐ์ฒด ์ง€ํ–ฅ ์ฟผ๋ฆฌ ์–ธ์–ด ์ œ๊ณต

- SQL๊ณผ ๋ฌธ๋ฒ• ์œ ์‚ฌ, SELECT, FROM, WHERE, GROUP BY, HAVING, JOIN ์ง€์›

- ์—”ํ‹ฐํ‹ฐ ๊ฐ์ฒด๋ฅผ ๋Œ€์ƒ์œผ๋กœ ์ฟผ๋ฆฌ

- SQL์€ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ํ…Œ์ด๋ธ”์„ ๋Œ€์ƒ์œผ๋กœ ์ฟผ๋ฆฌ

- SQL์„ ์ถ”์ƒํ™”ํ•ด์„œ ํŠน์ • DB SQL์— ์˜์กด X

- ๊ฐ์ฒด ์ง€ํ–ฅ SQL

 

  • ๋„ค์ด๋ฒ„ ๋ธ”๋Ÿฌ๊ทธ ๊ณต์œ ํ•˜๊ธฐ
  • ๋„ค์ด๋ฒ„ ๋ฐด๋“œ์— ๊ณต์œ ํ•˜๊ธฐ
  • ํŽ˜์ด์Šค๋ถ ๊ณต์œ ํ•˜๊ธฐ
  • ์นด์นด์˜ค์Šคํ† ๋ฆฌ ๊ณต์œ ํ•˜๊ธฐ