본문 바로가기
Study/JPA

[JPA] JPA 프로젝트 설정

by DevJaewoo 2022. 2. 26.
반응형

JPA Hibernate LOGO

JPA란?

Java Persistence API의 약자로, 자바의 ORM 기술의 표준이다.

즉, JPA는 ORM을 위한 인터페이스 같은 개념이고, 이 인터페이스를 구현만 하면 JPA로 사용할 수 있다.

예를 들자면, 우리가 아는 대표적인 JPA 구현체로는 Hibernate가 있지만, EclipseLinkDataNucleus 같이 JPA 인터페이스를 구현한 다른 구현체들도 Hibernate 대신 교체해서 쓸 수 있는 것이다.


JPA 프로젝트 생성

[File] - [New Project] 를 선택하고 Maven 또는 Gradle 프로젝트를 생성한다.

그 후 JPA를 사용하기 위해 Dependency를 추가해준다.

이번 프로젝트의 경우 JPA의 구현체 중 Hibernate를 선택하도록 하겠다.

Maven 프로젝트의 경우 pom.xml을, Gradle 프로젝트의 경우 build.gradle을 수정하면 된다.

 

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-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>5.6.5.Final</version>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>2.1.210</version>
        </dependency>
    </dependencies>

</project>

 

build.gradle

dependencies {
    ...
    implememtation 'org.hibernate:hibernate-entitymanager:5.6.5.Final'
    runtimeOnly 'com.h2database:h2:2.1.210' 
    ...
}

 

그 다음 JPA 사용을 위한 속성들을 정의해줘야 한다.

Database Driver, ID/PW, URL, Dialect가 필수적으로 들어가고, SQL 콘솔 출력과 같은 옵션들도 정의되어 있다.

Dialect방언이란 뜻을 가지고 있는데, DB마다 다 다른 문법을 어떤 DB의 방언인지만 정의해주면 JPA가 알아서 번역하여 SQL을 작성해준다.

 

dialect 구성도

 

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/~/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>

 

이제 Main 클래스를 만들고 JPA가 제대로 Import 되는지 테스트해보자.

코드에서 사용되는 클래스들은 모두 정상적으로 import 되어야 한다.

 

JpaMain.java

public class JpaMain {
    public static void main(String[] args) {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("hello");
        EntityManager em = emf.createEntityManager();
        EntityTransaction tx = em.getTransaction();
        
        tx.begin();

        //code

        tx.commit();
        
        em.close();
        emf.close();
    }
}

 

잘 실행된다.

로그가 빨간색이라 에러처럼 보일 수도 있는데 자세히 보면 INFO라고 써있다.

실행 결과

 

코드 분석은 다음 시간에 하겠다.


출처

반응형

'Study > JPA' 카테고리의 다른 글

[JPA] 상속관계 매핑  (0) 2022.08.26
[JPA] 연관관계 매핑  (0) 2022.08.25
[JPA] Entity 필드, 컬럼 매핑  (0) 2022.03.05
[JPA] 영속성 컨텍스트  (0) 2022.03.05
[JPA] DB에 데이터 넣어보기  (0) 2022.02.26