源自:willpower (個人網站) 标簽:java
今天開始Hibernate3之旅,在Hibernate2的基礎上改進了不少,讓我們一起借助這本書來學習吧。
今天開始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的持久化操作
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
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
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;
}
大家都看到了,這段代碼包括資料庫連接配接建立,釋放資源等。對于每個查詢的方法,都有這樣的代碼,其實這個顯得很備援。當然了,我們也可以對它進行重構,将這部分代碼提取出來,提供一些樣本方法。