본문 바로가기

jpa

JPA 프로젝트 개요 및 생성

프로젝트 환경

  • Java 8
  • Spring Boot
  • JPA
  • H2 DB 1.4.199
  • Maven

실행 사항

  1. H2 데이터베이스 이용한다.
  2. 프로젝트를 생성한다.
  3. xml 설정을 한다.
  4. main 클래스를 생성한다.
  5. main 클래스를 실행한다.

요구 조건

  1. 회원 등록
  2. 회원 수정
  3. 회원 삭제
  4. 회원 단건 조회

상세 구현 요건 및 제약 사항

1. 프로젝트 생성

2. xml 설정

  • pom.xml에 라이브러리 추가
    • JPA하이버네이트 디펜던시 추가
    • H2데이터베이스 디펜던시 추가
    <?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.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>
  • persistence.xml
  • jpa 설정파일
  • /META-INF/persistence.xml 위치
  • persistence-unit name으로 이름 지정
  • javax.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"/>

3. JpaMain 클래스 생성

  • Jpa Code를 수행하는 클래스를 생성한다.

4. 프로젝트 구현 내용

  • Jpa에서 H2 Database에 연결하여 CRUD 동작을 확인한다.

모델 구조

Member
| Type | 값 (예) | KEY |
| -------- | ---------------- | --- |
| Long | id | Y |
| String | Name | |

create table Member (
 id bigint not null,
 name varchar(255),
 primary key (id)
);

'jpa' 카테고리의 다른 글

JPA 영속성  (0) 2021.03.14
JPA EntityFactory 활용 CRUD  (0) 2021.03.01
JPA 개요  (0) 2021.02.22