/**
作者:Willpower
來源:Rifoo Technology(
http://www.rifoo.com
)
時間:2006-05
備注:轉載請保留以上聲明
**/
今天開始Hibernate3之旅,在Hibernate2的基礎上改進了不少,讓我們一起借助這本書來學習吧。
本書分兩個部分,第一部分是Hibernate入門知識(第1到4章),第二部分是Hibernate高級知識(第5到14章)。
我們今天來看看第一章的內容:Hibernate 3的介紹。
大多數重大的開發項目都會涉及到關系數據庫的概念。許多商業應用的核心就是對規則有序的信息進行大規模的存儲,比如目錄,客戶列表,合同資料等。
隨著互聯網的繁榮,對數據庫的要求越來越高。在線書店的那些客戶們可能自己都不知道,自己每一次的查詢操作或者點擊某個按鈕都會去操作數據庫。
隨著應用程序的需求不段提高,后來出現了標準的EJB規范,EJB規范提供了容器和Bean管理的CMP。但是,這個復雜性很高,而性能卻并沒有想像中的高。后來,Hibernate的出現給數據的持久化帶來了很大的轟動。針對一些數據庫持久化解決方案,有用EJB的,有用傳統JDBC的,有用Hibernate的。但Hibernate在易用性上顯示了突出的優勢。
這一章我們會提供給大家一個“Hello world”數據庫范例。該范例能夠通過一個簡單的關鍵字來查找并顯示數據庫中被保存的一個message信息。下面我們會提供原始JDBC寫法和Hibernate寫法:
Listing 1-1. 這個Main方法會調用我們Hibernate或JDBC的持久化操作
CODE:
public static void main(String[] args) {
? if (args.length != 1) {
? ? System.err.println("Nope, enter one message number");
? } else {
? try {
? ? int messageId = Integer.parseInt(args[0]);
? ? //這一句是調用的方法
? ? [b]Motd motd = getMotd(messageId);[/b]
? ? if (motd != null) {
? ? ? ? System.out.println(motd.getMessage());
? ? } else {
? ? ? ? System.out.println("No such message");
? ? }
? } catch (NumberFormatException e) {
? ? System.err.println("You must enter an integer - " + args[0]
? ? ? ? + " won't do.");
? } catch (MotdException e) {
? ? System.err.println("Couldn't get the message: " + e);
? ? }
? }
}
在理想的世界中,我們將很容易的獲取任何JAVA對象并將它持久化到數據庫。不需要編寫額外的特殊代碼。也能夠保證有良好的性能。對象關系映射(Object Relational Mapping)技術能持久化傳統的JAVA對象,而這里的POJO(Plain Old Java Objects)也是一種可重用的JAVA對象,POJO可以是任意的JAVA對象,而不需要Entity Bean那樣的命名約束。Hibernate能夠幫助我們很輕松地去持久化POJO。
下面我們編寫POJO部分:
Listing 1-2. 本范例中使用到的POJO
CODE:
public class Motd {
? protected Motd() {
? }
? public Motd(int messageId, String message) {
? ? this.messageId = messageId;
? ? this.message = message;
? }
? public int getId() {
? ? return id;
? }
? public void setId(int id) {
? ? this.id = id;
? }
? public String getMessage() {
? ? return message;
? }
? public void setMessage(String message) {
? ? this.message = message;
? }
? private int messageId;
? private String message;
}
下面是從數據庫中檢索Motd對象的傳統JDBC的寫法,代碼如下:
Listing 1-3. 檢索POJO
CODE:
public static Motd getMotd(int messageId) throws MotdException {
? Connection c = null;
? PreparedStatement p = null;
? Motd message = null;
? try {
? Class.forName("org.postgresql.Driver");
? c = DriverManager.getConnection(
? ? "jdbc:postgresql://127.0.0.1/hibernate",
? ? "hibernate",
? ? "hibernate");
? p = c.prepareStatement(
? "select message from motd where id = ?");
? p.setInt(1, messageId);
? ResultSet rs = p.executeQuery();
? ? if (rs.next()) {
? ? ? ? String text = rs.getString(1);
? ? ? ? message = new Motd(messageId, text);
? ? ? ? if (rs.next()) {
? ? ? ? ? log.warning(
? ? ? ? ? "Multiple messages retrieved for message ID: "
? ? ? ? ? + messageId);
? ? ? ? ? }
? ? ? ? }
? ? } catch (Exception e) {
? ? log.log(Level.SEVERE, "Could not acquire message", e);
? ? throw new MotdException(
? ? "Failed to retrieve message from the database.", e);
? } finally {
? ? if (p != null) {
? ? ? ? try {
? ? ? ? ? p.close();
? ? ? ? } catch (SQLException e) {
? ? ? ? ? log.log(Level.WARNING,
? ? ? ? ? "Could not close ostensibly open statement.", e);
? ? ? ? }
? ? }
? ? if (c != null) {
? ? ? ? try {
? ? ? ? ? c.close();
? ? ? ? } catch (SQLException e) {
? ? ? ? log.log(Level.WARNING,
? ? ? ? "Could not close ostensibly open connection.", e);
? ? ? ? }
? ? }
? } ?
return message;
}
大家都看到了,這段代碼包括數據庫連接建立,釋放資源等。對于每個查詢的方法,都有這樣的代碼,其實這個顯得很冗余。當然了,我們也可以對它進行重構,將這部分代碼提取出來,提供一些樣本方法。
posted on 2006-07-28 09:55
水煮三國 閱讀(449)
評論(0) 編輯 收藏 所屬分類:
Hibernate