4.Interceptor 介面:
您可以在开启Session时载入一个自订Interceptor,这个Interceptor会在对应的动作发生之前呼叫对应的方法,方法是让您定义的Interceptor实作Interceptor介面,介面的定义如下:
Interceptor.javapackage 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.javapackage 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 在hibernate.cfg.xml中设定JDBC等相关设定:hibernate.hbm.xml true org.hibernate.dialect.MySQLDialect com.mysql.jdbc.Driver jdbc:mysql://localhost/demo caterpillar 123456 5 20 1800 50
可撰写一个程式如下:
HbmToTable.javapackage 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 usercreate 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)
每天告诉自己一次,我真的很不错