Hibernate 3學習筆記 - 簡單的Persistent class和mapping file

 
首先我們來看一個簡單的Hibernate持久化的類:
import java.util.Date;
 
public class Event {
private Long id;
private String title;
private Date date;
 
public Event() {}
 
public Long getId() {
return id;
}
 
private void setId(Long id) {
this.id = id;
}
 
public Date getDate() {
return date;
}
 
public void setDate(Date date) {
this.date = date;
}
 
public String getTitle() {
return title;
}
 
public void setTitle(String title) {
this.title = title;
}
}
 
大家可以看到,這就是一個簡單的POJO. 這個例子遵循了標準的JavaBean的命名習慣,所有的成員變量都是private的,每個成員變量都有相應的getter,setter方法。實際上這並不是Hibernate的要求,Hibernate也可以直接訪問成員變量(property)。Hibernate可以訪問public,protected和private的方法,也可以直接訪問public,protected,private的成員變量。開發人員可以依據實際情況決定使用那種方式。
在上面的例子中,id成員變量代表的是可以唯一確定一個Event實例的屬性(通常來說是數據庫表中的主鍵)。Hibernate要求持久化的類都必須有這樣的成員變量(並不是一定要叫id)。
同時,Hibernate還要求持久化的類必須有一個無參數的構造函數,以便Hibernate利用反射來生成該類的實例。該構造函數可以是private的,但是如果要生產運行時的代理,或者要考慮代碼的效率,至少要保證包一級的可見(也就是protected)。
只有持久化的類還不夠,Hibernate必須要知道怎樣保存和載入持久化類的對象。Hibernate使用映射文件(mapping file)來定義這些信息。mapping file會告訴Hibernate使用數據庫中的什麼表和表中的哪些列,以及這些列和類中的成員變量的對應關係。Mapping file的基本格式如下:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
[...]
</hibernate-mapping>
對於以上的Event類,相應的Mapping file如下:
<hibernate-mapping>
<class name="events.Event" table="EVENTS">
<id name="id" column="EVENT_ID">
               <generator class="native"/>
</id>
<property name="date" type="timestamp" column="EVENT_DATE"/>
<property name="title"/>
</class>
</hibernate-mapping>
 
在這裏,class定義了java類與數據庫表的對應關係。其中name屬性是java類的全限定名(包含包名的類名)。table屬性是數據庫中的表名(如有必要,可以包含schema的名稱)。
class的子元素id定義了可以唯一標識這個類的成員變量(前面提過了,Hibernate要求每個持久化類都包含一個這樣的成員變量)。name屬性指明瞭該成員變量的名字,column屬性指明瞭該成員變量對應表的哪個column。generator元素指明瞭生成該ID的方法。這裏native表明由Hibernate來決定生成主鍵的方式,Hibernate會根據使用的數據庫的類型來選擇最合適的生產主鍵的方式。
每個property元素對應Event類中的一個成員變量。property元素的name屬性指明瞭Event中的成員變量名稱;type屬性告訴Hibernate該成員變量的類型;Column屬性告訴Hibernate該成員變量對應數據庫表中的哪個Column。type屬性使用的並不是Java中的數據類型,也不是SQL的數據庫數據類型,而是Hibernate自己定義的,叫做Hibernate mapping type(Hibernate映射類型)。該類型定義了從Java數據類型到SQL數據類型的對應關係。
在上面的mapping file中,沒有直接指明title成員變量對應的數據類型和column的名稱,這樣Hibernate會使用缺省值。對於column,Hibernate會使用成員變量的名稱作爲column的名稱。對於type,Hibernate會根據Java類中該成員變量的類型來決定使用哪種Hibernate mapping type。
 
附:
  1. id的generator可以爲以下的值:
  • increment :generates identifiers of type long, short or int that are unique only when no other process is inserting data into the same table. Do not use in a cluster.
  • identity:supports identity columns in DB2, MySQL, MS SQL Server, Sybase and HypersonicSQL. The returned identifier is of type long, short or int.
  • sequence :uses a sequence in DB2, PostgreSQL, Oracle, SAP DB, McKoi or a generator in Interbase. The returned identifier is of type long, short or int
  • hilo :uses a hi/lo algorithm to efficiently generate identifiers of type long, short or int, given a table and column (by default hibernate_unique_key and next_hi respectively) as a source of hi values. The hi/lo algorithm generates identifiers that are unique only for a particular database.
  • seqhilo :uses a hi/lo algorithm to efficiently generate identifiers of type long, short or int, given a named database sequence.
  • uuid :uses a 128-bit UUID algorithm to generate identifiers of type string, unique within a network (the IP address is used). The UUID is encoded as a string of hexadecimal digits of length 32.
  • guid :uses a database-generated GUID string on MS SQL Server and MySQL.
  • native:picks identity, sequence or hilo depending upon the capabilities of the underlying database.
  • assigned :lets the application to assign an identifier to the object before save() is called. This is the default strategy if no <generator> element is specified.
  • select :retrieves a primary key assigned by a database trigger by selecting the row by some unique key and retrieving the primary key value.
  • foreign :uses the identifier of another associated object. Usually used in conjunction with a <one-to-one> primary key association.
  • sequence-identity :a specialized sequence generation strategy which utilizes a database sequence for the actual value generation, but combines this with JDBC3 getGeneratedKeys to actually return the generated identifier value as part of the insert statement execution. This strategy is only known to be supported on Oracle 10g drivers targetted for JDK 1.4. Note comments on these insert statements are disabled due to a bug in the Oracle drivers.
 
  1. type可以爲以下的值:
  • Hibernate基本類型,例如 integer, string, character, date, timestamp, float, binary, serializable, object, blob.
  • Java中有缺省的基本類型的類,例如int, float, char, java.lang.String, java.util.Date, java.lang.Integer, java.sql.Clob.
  • 實現了Seriliazable接口的Java類.
  • 用戶自定義的類型的類名。(關於用戶自定義的類型,以後會學習到)。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章