Web/JPA

EP1. JPA 개념과 개발환경 세팅

요즘 시대는 객체지향프로그래밍과 관계형데이터베이스가 대부분이다.

 

그래서 아래 2가지 문제가 생긴다.

1. SQL 의존적인 문제

2. 객체 vs 관계형 DB 의 패러다임 불일치 ( 객체 -> SQL, SQL -> 객체 무한변환..)

 

 

객체와 관계형 데이터베이스의 차이

  객체 데이터베이스
상속 있음 없음
연관관계 참조를 통해 가져오기 가능 join을 통해 가져와야함

 

데이터베이스와의 연동을 객체지향프로그래밍 원리에 맞추려고 하면 할 수록

자바의 객체와 SQL의 객체의 차이때문에 더 복잡해지기 시작했다.

 

객체를 자바 컬렉션에 저장하듯이 DB에 저장할 수는 없을까?

-> JPA 등장

 

JPA : Java Persistence API

자바 진영의 ORM 기술 표준

 

ORM : Object-Relational Mappring (객체 관계 매핑)

객체는 객체대로, 관계형DB는 관계형DB답게 설계하고 ORM이 알아서 매핑해줄게

 

 

추가로 궁금해서 알아본 데이터베이스의 종류

1. 관계형 데이터베이스 : 엑셀처럼 column과 row로 표현되는 테이블로 작성

2. NoSQL : 여러 방식으로 데이터 저장 (Document, key-value, Big Table ...)

3. 계층형 데이터베이스 : 트리구조 모델

4. 네트워크형 데이터베이스 : 그물망 구조 모델

 

 

 

개발환경 세팅

maven 설정파일인 pom.xml 작성

JPA 하이버네이트와 h2 데이터베이스를 가져왔다.

 

 

 

META-INF directory를 만들고

persistence.xml 파일안에서 jdbc의 설정과 옵션을 켜주었다.

 

Jpa가 제대로 동작하는지 확인하기위해 JpaMain을 작성했다.

 

그런데 

 

WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)

Exception in thread "main" org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]

...중략

 

에러가 발생했다.

 

해결 : 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>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <!-- JPA 하이버네이트 -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>5.4.1.Final</version>
        </dependency>

        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.3.1</version>
        </dependency>

        <!-- H2 데이터베이스 -->
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>1.4.199</version>
        </dependency>

    </dependencies>

</project>

하이버네이트 버전과 maven 컴파일러 부분의 호환상 문제가 생긴거 같았다.