본문 바로가기
JPA Tutorial

[자바 ORM 표준 JPA 프로그래밍 3] Hello JPA - 프로젝트 생성

by 미소5 2023. 9. 13.
  • 프로젝트 생성
    1.  File > New > Project > Maven 선택
    2. 메이븐 설정
      • groupId: jpa-basic
      • artifactId: ex1-hello-jpa
      • version: 1.0.0

메이븐 설정

 


  •  pom.xml 에 라이브러리 추가 
<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>

 


  • JPA 설정하기 - JPA 설정 파일 persistence.xml
    • 위치(표준 위치 정해져있음)resources/META-INF/persistence.xml
<?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/~/jpashop"/>
            <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>
  •  persistence-unit name 으로 이름 지정
  •  javax.persistence 로 시작: JPA 표준 속성
  •  hibernate 로 시작: 하이버네이트 전용 속성

 


  • JPA는 특정 데이터베이스에 종속 X
  • 각각의 데이터베이스가 제공하는 SQL 문법과 함수는 조금씩 다름
    • 가변 문자: MySQL은 VARCHAR, Oracle은 VARCHAR2
    • 문자열을 자르는 함수: SQL 표준은 SUBSTRING(), Oracle은 SUBSTR()
    • 페이징: MySQL은 LIMIT , Oracle은 ROWNUM

데이터베이스 방언

 

  • 데이터베이스 방언 (hibernate.dialect)
    • 방언: SQL 표준을 지키지 않는 특정 데이터베이스만의 고유한 기능
    • hibernate.dialect 속성에 지정
      • H2 : org.hibernate.dialect.H2Dialect
      • Oracle 10g : org.hibernate.dialect.Oracle10gDialect
      • MySQL : org.hibernate.dialect.MySQL5InnoDBDialect
    • 하이버네이트는 40가지 이상의 데이터베이스 방언 지원

 


[Spring 입문9] 스프링 DB 접근 기술: H2 데이터베이스 설치 (tistory.com)

 

[Spring 입문9] 스프링 DB 접근 기술: H2 데이터베이스 설치

h2 데이터베이스 1.4.200 버전을 설치 https://www.h2database.com/html/download-archive.html Archive Downloads www.h2database.com h2.bat 실행 또는 http://localhost:8082 접속 최초 한번 연결 JDBC URL: jdbc:h2:~/test ~/test.mv.db 파일

joly156.tistory.com

 

 

JDBC URLpersistence.xml 파일의 다음 설정과 같아야 한다.

<property name="javax.persistence.jdbc.url" value="jdbc:h2:tcp://localhost/~/jpashop"/>

 


  • JPA 구동 방식
  1. persistence라는 클래스에서 설정정보(persistence.xml) 조회해서
  2. EntityManagerFactory라는 클래스 생성
  3. 이 EntityManagerFactory클래스에서 필요할때마다 EntityManager 생성해서 사용

 


  • JPA 동작 확인
    • JpaMain 클래스 생성
package hellojpa;

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

public class JpaMain {

    public static void main(String[] args){

        EntityManagerFactory emf=Persistence.createEntityManagerFactory("hello");
        //persistence.xml에서 persistence-unit name 으로 지정한 이름 "hello"

        EntityManager em=emf.createEntityManager();

        //code

        em.close();
        emf.close();

    }
}

 

728x90
반응형