* 모든 back-slash를 slash로 변경했음
압축파일내 hibernate3.jar와 lib내의 모든 파일을 자신의 lib디렉토리에 복사 오라클 JDBC_드라이버(ojdbc14.jar)를 복사 D:/work>mkdir Hibernatetest/src D:/work>mkdir Hibernate_test/lib D:/work>unzip hibernate-3.1.3.zip D:/work>copy hibernate-3.1/hibernate3.jar Hibernate_test/lib D:/work>copy hibernate-3.1/lib/* Hibernate_test/lib D:/work>copy hibernate-3.1/etc/log4j.properties Hibernate_test/src D:/work>copy C:/oraclexe/app/oracle/product/10.2.0/server/jdbc/lib/ojdbc14.jar Hibernate_test/lib
테스트용 테이블 생성SQL> conn test/test@devSQL> create table event( 2 event_id integer primary key, 3 title varchar(20), 4 event_date date);
[src/Event.java] - 영속 클래스(persistent class) import java.util.Date;
public class Event{ private Long id; private String title; private Date date;
Event(){}
public Long getId(){ return id; }
public String getTitle(){ return title; }
public Date getDate(){ return date; }
private void setId(Long id){ // id는 key로 쓰이므로 public접근을 막음 this.id = id; }
public void setTitle(String title){ this.title = title; }
public void setDate(Date date){ this.date = date; } }
<hibernate-mapping> <class name="Event" table="EVENT"> <id name="id" column="EVENT_ID"> <generator class="increment"/> </id> <property name="date" type="timestamp" column="EVENT_DATE"/> <property name="title"/> </class> </hibernate-mapping>
<hibernate-configuration> <session-factory>
<property name="connection.driver_class">oracle.jdbc.OracleDriver</property> <property name="connection.url">jdbc:oracle:thin:@localhost:1555:dev</property> <property name="connection.username">test</property> <property name="connection.password">test</property>
<property name="connection.pool_size">1</property> <property name="show_sql">true</property> <mapping resource="Event.hbm.xml"/>
</session-factory> </hibernate-configuration>
[src/HibernateUtil.java] - 사용 편의를 위한 Helper 클래스 import org.hibernate.*; import org.hibernate.cfg.*;
public class HibernateUtil{
public static final SessionFactory sessionFactory;
static{ try{ sessionFactory = new Configuration().configure().buildSessionFactory(); }catch(Throwable ex){ System.err.println("Initial SessionFactory creation failed." +ex); throw new ExceptionInInitializerError(ex); } }
public static final ThreadLocal session = new ThreadLocal();
public static Session getCurrentSession() throws HibernateException{ Session s = (Session)session.get(); if(s == null){ s = sessionFactory.openSession(); session.set(s); } return s; }
public static void closeSession() throws HibernateException{ Session s= (Session)session.get(); if(s != null) s.close(); session.set(null); } }
[src/EventManager.java] - 메인 클래스 import org.hibernate.Transaction; import org.hibernate.Session; import java.util.Date; import java.util.List;
public class EventManager{ public static void main(String args[]){ EventManager mgr = new EventManager(); if(args[0].equals("store")){ mgr.createAndStoreEvent("My Event", new Date()); }else if(args[0].equals("list")){ List<Event> events = mgr.listEvents(); for(Event m : events) System.out.println("Event: "+m.getTitle()+", Time: "+m.getDate()); }
HibernateUtil.sessionFactory.close(); }
private void createAndStoreEvent(String title, Date date){ Session session = HibernateUtil.getCurrentSession(); Transaction tx = session.beginTransaction();
Event event = new Event(); event.setTitle(title); event.setDate(date);
session.save(event); tx.commit(); HibernateUtil.closeSession(); }
private List listEvents(){ Session session = HibernateUtil.getCurrentSession(); Transaction tx = session.beginTransaction();
List result = session.createQuery("from Event").list(); tx.commit(); session.close();
return result; } }
[build.xml] <project name="hibernate-test" default="compile" basedir=".">
<property name="srcdir" value="src"/> <property name="bindir" value="bin"/> <property name="libdir" value="lib"/>
<path id="libraries"> <fileset dir="${libdir}"> <include name="*.jar"/> </fileset> </path>
<target name="clean"> <delete dir="${bindir}"/> </target>
<target name="copy-resources"> <mkdir dir="${bindir}"/> <copy todir="${bindir}"> <fileset dir="${srcdir}"> <exclude name="**/*.java"/> </fileset> </copy> </target>
<target name="compile" depends="copy-resources"> <javac srcdir="${srcdir}" destdir="${bindir}" classpathref="libraries"/> </target>
<target name="run" depends="compile"> <java classname="EventManager" classpathref="libraries"> <classpath path="${bindir}"/> <arg value="${action}"/> <arg value="${name}"/> </java> </target>
|