4.Interceptor 介面:
您可以在開啟Session時載入一個自訂Interceptor,這個Interceptor會在對應的動作發生之前呼叫對應的方法,方法是讓您定義的Interceptor實作Interceptor介面,介面的定義如下:
Interceptor.java
package org.hibernate;
import java.io.Serializable;
import java.util.Iterator;
import org.hibernate.type.Type;
public interface Interceptor {
// 載入物件之前執行
public
boolean onLoad(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types)
throws CallbackException;
// flush 時,如果發現有Dirty data,則執行此方法
public
boolean onFlushDirty(Object entity, Serializable id, Object[] currentState, Object[] previousState,
String[] propertyNames, Type[] types) throws CallbackException;
// 儲存物件前執行
public
boolean onSave(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types)
throws CallbackException;
// 刪除物件前執行
public
void onDelete(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types)
throws CallbackException;
// 在 flush 前執行
public void preFlush(Iterator entities) throws CallbackException;
// 在 flush 後執行
public void postFlush(Iterator entities) throws CallbackException;
// 判斷傳入的物件是否為 transient 狀態
public Boolean isTransient(Object entity);
// flush 前呼叫這個方法判斷 Dirty data
// 傳回Dirty data屬性索引或null采預設行為
public
int[] findDirty(Object entity, Serializable id, Object[] currentState, Object[] previousState,
String[] propertyNames, Type[] types);
// 手動建立實體物件,如果傳回 null,則使用預設的建構方法建立實例
public Object instantiate(String entityName, EntityMode entityMode, Serializable id)
throws CallbackException;
// 傳回實體名稱
public String getEntityName(Object object) throws CallbackException;
// 取得實體物件
public Object getEntity(String entityName, Serializable id) throws CallbackException;
// beginTransaction() 之後執行
public void afterTransactionBegin(Transaction tx);
// 在事務完成前執行
public void beforeTransactionCompletion(Transaction tx);
// 在事務完成後執行
public void afterTransactionCompletion(Transaction tx);
}
假設您實作了SomeInterceptor類別:
SomeInterceptor.java
package onlyfun.caterpillar;
....
public class SomeInterceptor implements Interceptor {
....
}
在開啟Session時,可以如下載入自訂的Interceptor:
SomeInterceptor someInterceptor = new SomeInterceptor();
Session session = sessionFactory.openSession(someInterceptor);
....
5.從映射文件生成數據表:
在您撰寫好*.hbm.xml映射文件之後,您可以使用org.hibernate.tool.hbm2ddl.SchemaExport來自動建立資料庫表格,假設您的User.hbm.xml如下:
User.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="onlyfun.caterpillar.User"
table="user">
<id name="id" column="id" type="java.lang.Integer">
<generator class="native"/>
</id>
<property name="name" column="name" type="java.lang.String"/>
<property name="age" column="age" type="java.lang.Integer"/>
</class>
</hibernate-mapping>在hibernate.cfg.xml中設定JDBC等相關設定:
hibernate.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 顯示實際操作資料庫時的SQL -->
<property name="show_sql">true</property>
<!-- SQL方言,這邊設定的是MySQL -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- JDBC驅動程式 -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- JDBC URL -->
<property name="connection.url">jdbc:mysql://localhost/demo</property>
<!-- 資料庫使用者 -->
<property name="connection.username">caterpillar</property>
<!-- 資料庫密碼 -->
<property name="connection.password">123456</property>
<!-- C3P0 連接池設定 -->
<property name="c3p0.min_size">5</property>
<property name="c3p0.max_size">20</property>
<property name="c3p0.timeout">1800</property>
<property name="c3p0.max_statements">50</property>
<!-- 物件與資料庫表格映射文件 -->
<mapping resource="onlyfun/caterpillar/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
可撰寫一個程式如下:
HbmToTable.java
package onlyfun.caterpillar;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
public class HbmToTable {
public static void main(String[] args) {
Configuration config = new Configuration().configure();
System.out.println("Creating tables...");
SchemaExport schemaExport = new SchemaExport(config);
schemaExport.create(true, true);
}
}
運行程式之後,將會有以下的結果:
Creating tables...
10:39:10,203 DEBUG SchemaExport:143 - drop table if exists user
create table user (
id integer not null auto_increment,
name varchar(255),
age integer,
primary key (id)
)
10:39:10,203 DEBUG SchemaExport:161 - create table user (
id integer not null auto_increment,
name varchar(255),
age integer,
primary key (id)
)
10:39:10,359 INFO SchemaExport:173 - schema export complete
生成的資料表如下:
+--------+-----------------+------+------+----------+---------------------+
| Field | Type | Null | Key | Default | Extra |
+--------+-----------------+------+------+----------+---------------------+
| id | int(11) | | PRI | NULL | auto_increment |
| name | varchar(255) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
+--------+-----------------+------+------+----------+---------------------+
3 rows in set (0.00 sec)