도커로 H2 생성
docker run -d -p 1521:1521 -p 81:81 -v D:\\database:/opt/h2-data -e H2_OPTIONS='-ifNotExists' --name=h2db oscarfonts/h2
난 도커를 사용
docker run -d -p 1521:1521 -p 81:81 -v D:\\database:/opt/h2-data -e H2_OPTIONS='-ifNotExists' --name=h2db oscarfonts/h2
PS D:\\downloads\\Kitematic-Windows> docker run -d -p 1521:1521 -p 81:81 -v D:\\database:/opt/h2-data -e H2_OPTIONS='-ifNotExists' --name=h2db oscarfonts/h2
63ad35bad70054b400a46a4439bb6566420473a070dca759b45bc15be3cca95d
PS D:\\downloads\\Kitematic-Windows> docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
63ad35bad700 oscarfonts/h2 "/bin/sh -c 'java -c…" 4 seconds ago Up 2 seconds 0.0.0.0:81->81/tcp, 0.0.0.0:1521->1521/tcp h2db
PS D:\\downloads\\Kitematic-Windows>
메이븐 프로젝트 생성 및 의존성 추가
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.3.10.Final</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.199</version>
</dependency>
</dependencies>
JPA 설정
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"/>
<!-- 옵션 -->
<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>
그리고 일단 실행해보자.
package hellojpa;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
public class JpaMain {
public static void main(String[] args) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("hello");
}
}