목차(환경설정에 필요한 것)
1. 외장 라이브러리 관리 - Maven 에서 Hibernate-core 와 derby db 설정
2. 하이버네이트 설정파일 : hibernate.cfg.xml
준비물 ( 혹은 개발환경 ) : Eclipse EE Luna 버젼. Java 1.8 버젼에서 작성 중입니다.
이클립스 환경설정 General - workspace- utf-8환경입니다.
이클립스 환경설정 General - workspace- utf-8환경입니다.
1. 외장 라이브러리 관리
영상참조. 메이븐으로 외부 라이브러리를 불러들입니다. Junit까지 불러온 Pom.xml 은 다음과 같습니다.
derby DB가 싫으신 분들은 다른 Database를 쓰셔도 됩니다 :) Database설정은 영상을 참조해주세요.
<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>com.arahansa</groupId>
<artifactId>HibernateTutorial</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.3.6.Final</version>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derbyclient</artifactId>
<version>10.11.1.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
</dependencies>
</project>
더비 서버 실행파일이 있는 주소는 c:\Program Files\Java\설치된 자바폴더\db\bin 입니다.
2. 하이버네이트 설정파일
hibernate.cfg.xml 이란 파일을 src/main/resources 폴더에 만들어넣습니다. 다음과 같습니다.
mapping class 부분에 미리 클래스 설정을 해두었습니다
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">org.apache.derby.jdbc.ClientDriver</property>
<property name="connection.url">jdbc:derby://localhost:1527/HibernateDb;create=true</property>
<property name="connection.username">user</property>
<property name="connection.password">password</property>
<property name="dialect">org.hibernate.dialect.DerbyDialect</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- Enable Hibernate's current session context -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">create</property>
<mapping class="chap01.crud.Member" />
</session-factory>
</hibernate-configuration>
3. HibernateUtil 파일(세션 팩토리 제공)
util패키지를 만들고 세션팩토리를 제공하는 HibernateUtil 클래스를 만듭니다. 소스는 다음과 같습니다.
package util;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.boot.registry.StandardServiceRegistry;
/**
* @author Deepak Kumar * Web: http://www.roseindia.net
* Update by arahansa@naver.com
*/
public class HibernateUtil {
private static SessionFactory sessionFactory;
private static String configFile = "hibernate.cfg.xml";
static {
try {
Configuration cfg = new Configuration().configure(configFile);
StandardServiceRegistryBuilder sb = new StandardServiceRegistryBuilder();
sb.applySettings(cfg.getProperties());
StandardServiceRegistry standardServiceRegistry = sb.build();
sessionFactory = cfg.buildSessionFactory(standardServiceRegistry);
} catch (Throwable th) {
System.err.println("Enitial SessionFactory creation failed" + th);
throw new ExceptionInInitializerError(th);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
public void shutdown() {
sessionFactory.close();
}
}
4. 참고주소
하이버네이트 4.3.6 버젼 하이버네이트 util 클래스를 참고하였습니다. 인도의 장미님께 감사를.
Deepak Kumar * Web: http://www.roseindia.net

