Hibernate 2.1.7c anchor.png Edit

  • ライブラリのインストール

以下のサイトよりライブラリをダウンロード
http://www.hibernate.org/

# cd /usr/local/arch
# wget http://voxel.dl.sourceforge.net/.../hibernate-2.1.7c.tar.gz
# cd ../src
# tar xvzf ../arch/hibernate-2.1.7c.tar.gz
# cd hibernate-2.1
# cp hibernate2.jar /home/test/WEB-INF/lib
# cd lib
# cp c3p0-0.8.4.5.jar /home/test/WEB-INF/lib
# cp cglib-full-2.0.2.jar /home/test/WEB-INF/lib
# cp commons-logging-1.0.4.jar /home/test/WEB-INF/lib
# cp dom4j-1.4.jar /home/test/WEB-INF/lib
# cp jta.jar /home/test/WEB-INF/lib

※他にも${TOMCAT}/common/libにあるcommons-collections-3.1.jarを使用する。

  • 設定
# cd /home/test/WEB-INF/classes
# vi hibernate.cfg.xml
<?xml version="1.0" ?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd" >
<hibernate-configuration>
<session-factory>
    <property name="connection.driver_class">org.gjt.mm.mysql.Driver </property>
    <property name="connection.url">jdbc:mysql://localhost:3306/test</property>
    <property name="connection.username">test</property>
    <property name="connection.password">test</property>
    <property name="dialect">net.sf.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.show_sql">true</property>
    <property name="c3p0.min_size">5</property>
    <property name="c3p0.max_size">20</property>
    <property name="c3p0.max_statements">50</property>
    <property name="c3p0.timeout">1800</property>
    <property name="c3p0.validate">true</property>
    <property name="hibernate.connection.provider_class">
       net.sf.hibernate.connection.C3P0ConnectionProvider
    </property>
    <mapping resource="TestData.hbm.xml"/>
  </session-factory>
</hibernate-configuration>
  • オブジェクトの作成
    • テーブル作成
CREATE TABLE
       test_data
(
       test_id                 INT             AUTO_INCREMENT  PRIMARY KEY,
       test_name               VARCHAR(20)     NOT NULL UNIQUE,
       reg_date                DATETIME        NOT NULL,
)      TYPE=MyISAM;
    • マッピングファイル作成
# cd /home/test/WEB-INF/classes
# vi TestData.hbm.xml
<?xml version="1.0" ?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
  <class name="com.domain.pojo.TestData" table="test_data">
    <id name="testId" column="test_id" type="int" >
      <generator class="native" />
    </id>
    <property name="testName" type="string" column="test_name" />
    <property name="regDate" type="date" column="reg_date" />
  </class>
</hibernate-mapping>
    • サンプルPOJOの作成
# mkdir /home/test/src/com/domain/pojo
# cd /home/test/src/com/domain/pojo
# vi TestData.java
package com.domain.pojo;
import java.io.Serializable;
import java.util.Date;
public class TestData implements Serializable{
  private Integer testId;
  private String testName;
  private Date regDate;
  public TestData(){}
  public Integer getTestId(){return this.testId;}
  public void setTestId(Integer testId){this.testId=testId;}
  public String getTestName(){return this.testName;}
  public void setTestName(String testName){this.testName=testName;}
  public Date getRegDate(){return this.regDate;}
  public void setRegDate(Date regDate){this.regDate=regDate;}
}
    • サンプルDAOの作成
# mkdir /home/test/src/com/domain/dao
# cd /home/test/src/com/domain/dao
# vi TestDataHbmDao.java
package com.domain.dao;
import java.util.List;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.cfg.Configuration;
import com.domain.pojo.TestData;
public class TestDataHbmDao{
  public TestDataHbmDao(){}
  public TestData[] getAllTestData() throws HibernateException{
    Configuration config=new Configuration();
    config=config.configure();
    SessionFactory sessionfactory=config.buildSessionFactory();
    Session session=sessionfactory.openSession();
    List list=session.createCriteria(TestData.class).list();
    TestData[] testDatas=new TestData[list.size()];
    list.toArray(testDatas);
    return testDatas;
  }
  public static void main(String[] args) throws HibernateException{
    TestData[] testDatas=new TestDataHbmDao().getAllTestData();
    for (int i=0; i<testDatas.length; i++){
      System.out.println(testDatas[i].getTestId()+":"
       +testDatas[i].getTestName());
    }
  }
}
# cd /home/test
# ant
# java com.domain.dao.TestDataHbmDao

トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   ページ新規作成 全ページ一覧 単語検索 最新ページの一覧   ヘルプ   最新ページのRSS 1.0 最新ページのRSS 2.0 最新ページのRSS Atom
Counter: 532, today: 1, yesterday: 0
最終更新: 2008-11-26 (水) 02:01:32 (JST) (1272d) by aqua
オススメ

おすすめ
カレンダ
前月2012年 5月翌月
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
<今日>
ログイン
ユーザ名:

パスワード:



パスワード紛失

新規登録
天気予報

資料提供天気予報コム
アクセスカウンタ
今日 : 157157157
昨日 : 577577577
総計 : 363711363711363711363711363711363711