JBoss-IDE 1.2.2 簡介 (Hibernate) 1:
先要條件:
l 要完成 JBoss-IDE 1.2.2 教程 (http://www.youkuaiyun.com/develop/Read_Article.asp?id=25298), 和成功運行當中的例子, FiboBean 和 ComputeServlet
l 對 JBoss, MySQL 和 Hibernate 有一定的認識及下載 Hibernate 2.1.2 (http://prdownloads.sourceforge.net/hibernate/hibernate-2.1.2.zip?download) 和 XDoclet (http://prdownloads.sourceforge.net/xdoclet/xdoclet-bin-1.2.zip?download)
說明:
l 我會繼續用 JBoss-IDE 教程裏的例子, 當中是一個 index.html call ComputeServlet 再 invoke FiboBean 中的計算斐波納契數列 (Fibonacci, 即一種整數數列,其中每數等於前面兩數之和) 的 function (compute). 而應用到 Hibernate 之後, 我會在 compute function 裏找出資料庫 (MySQL – test db) 中相關儲存計算 Fibonacci 結果後的資料 (Table - fibo), 如和輸入要求計算的數字是相同的話, 不用計算而在資料庫中拿取資料作為結果. 如不同即會清空資料庫中的資料, 再重新計算和存入資料庫中.
步驟:
1. 我會用 MySQL 創建一個 table 來儲存計算斐波納契數列的結果, 首先我會創建一個資料庫名命為 ‘test’, 再創建一個 table 名命為 ‘fibo’, 而 ‘fibo’ 只有兩個 fields, 一是 id (varchar, PK), 一是 value (double). 最後在 JBoss 的 default server 中設定這個 MySQL – test 的 DataSource.
2. 下載 Hibernate 2.1.2 後請參考網上有關怎樣設定 Hibernate 在 JBoss 裏的 sar 服務運行教程 (http://www.hibernate.org/66.html - 筆者按: 這條 link 好像死了, 不敢肯定, 可能Hibernate 已投入 JBoss 中, 而 Hibernate 會在 4.0 的版本取代 CMP, 所以其實這篇簡介不是太有用, 只可當作參考.) 和 (http://nemesisit.rdsnet.ro/opendocs/ejbhibernate.html). 複製 Hibernate 根目錄中的 hibernate2.jar 和 lib 資料夾中的 ‘cglib-2.0-rc2.jar’, ‘commons-collections-2.1.jar’, ‘commons-lang-1.0.1.jar’, ‘commons-logging-1.0.3.jar’, ‘dom4j-1.4.jar’, ‘jcs-1.0-dev.jar’, ‘odmg-3.0.jar’, 到 JBoss default server 的 lib (%JBOSS_HOME/server/default/lib) 資料夾中. 完成後到 XDoclet, 下載後爆開它, 複製其 lib資料夾中的 ‘xdoclet-hibernate-module-1.2b4.jar’ 到 eclipse資料夾中的 /plugins/org.jboss.ide.eclipse.xdoclet.core_1.2.2/ 資料夾裏, 因為這是外加 module 在 XDoclet 裏, 所以記得在 JBoss-IDE 的 XDoclet Configure 中更新一下 XDoclet modules 和 data.
3. 用 eclipse (我是用 2.1) 開啟 JBoss-IDE 教程裏 tutorial 的 project, 右擊 tutorial project, 選 ‘Properties’.
然後選 ‘Java Build Path’ -> ‘Libraries’ -> ‘Add External JARs’, 選 JBoss default folder 裏的 ‘lib’ (%JBOSS_HOME%/server/default/lib/), 選 ‘hibernate2.jar’, 按 ‘開啟’再按 ‘OK’.
4. 現在根據 Hibernate 跟來的教程來創建一個 HibernateUtil Class. 首先右擊 tutorial project -> ‘New’ -> ‘Class’, ‘Package’ 填入 ‘tutorial.hibernate’, Name 填入 ‘HibernateUtil’, 再按 ‘Finish’, 而代碼如下:
package tutorial.hibernate;
import javax.naming.Context;
import javax.naming.InitialContext;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.HibernateException;
public class HibernateUtil {
private static SessionFactory sessionFactory;
static {
try {
Context ctx = new InitialContext();
sessionFactory = (SessionFactory) ctx.lookup("java:/hibernate/HibernateFactory");
}
catch (Exception e) {
e.printStackTrace();
}
}
public static final ThreadLocal session = new ThreadLocal();
public static Session currentSession() throws HibernateException {
Session s = (Session) session.get();
// Open a new Session, if this Thread has none yet
if (s == null) {
s = sessionFactory.openSession();
session.set(s);
}
return s;
}
public static void closeSession() throws HibernateException {
Session s = (Session) session.get();
session.set(null);
if (s != null)
s.close();
}
}
JBoss-IDE 1.2.2 簡介 (Hibernate) 2:
5. 完成後我們會再創建一個 Num 的 Hibernate 屬性檔案來影射在資料庫中的 fibo table, 右擊 tutorial project -> ‘New’ -> ‘Class’, ‘Package’ 填入 ‘tutorial.hibernate’, Name 填入 ‘Num’, 再按 ‘Finish’. 當中會用到 XDoclet 中的 Hibernate 標籤, 因為這個例子用到Hibernate 標籤實在太簡單, 所以沒有特別為此創建 Hibernate 標籤的 Template. 而 Hibernate 的 id 生成會用 uuid, 代碼如下:
package tutorial.hibernate;
/**
* @author patrick.ip
*
* @hibernate.class table="fibo"
*/
public class Num {
private String id;
private double value;
public Num() {
}
/**
* @hibernate.id generator-class="uuid.hex"
*/
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
/**
* @hibernate.property
* @return double
*/
public double getValue() {
return value;
}
public void setValue(double value) {
this.value = value;
}
}
而應用這兩個 Classes 會在 FiboBean 的 compute 方法裏, 首先要在 FiboBean.java 加上 Hibernate 的 import:
package tutorial.ejb;
import java.rmi.RemoteException;
import java.util.Iterator;
import javax.ejb.EJBException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.ejb.CreateException;
// hibernate imports
import tutorial.hibernate.*;
import net.sf.hibernate.Query;
import net.sf.hibernate.Session;
import net.sf.hibernate.Transaction;
import net.sf.hibernate.HibernateException;