稻草誓言 2010-03-11
一、Hibernate开发流程三种模式:
1。 由Domain Object (领域对象)—>Mapping(映射文件)—>DB (映射文件)(官方推荐的开发流程)
我的第一个hibernate实例就是按这种模式来开发的。
2。 由DB开始,用工具生成Mapping和Domain Object(这种开发模式较多)
3。 由映射文件开始。
在具体开发时,可以根据需要选择适合的开发模式。
二。 Domain Object(POJO)类的一些限制
1。 默认的构造方法(即无参构造方法)必须有.(因为hibernate在创建实体对象时用到了java反射机制)(The no-argument constructor is a requirement for all persistent classes;Hibernate has to create objects for you, using Java Reflection.)
2。 标识符Id(主键)可选
3。 非final的,对hibernate懒加载有影响(可选).(因为一个类有final修饰时就不能再被继承,而懒加载的一些特性需要继承实体类来实现).
标准的POJO类如下:(来自Hibernate Reference)
package org.hibernate.tutorial.domain;
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;
}
}三、*.hbm.xml配置文件作用说明
*.hbm.xml配置文件就是用于说明实体对象与哪个数据库表中的记录相对应,以及实体对象中的各个属性分别对应表中的哪一列。不同性质的属性(例如主键和普通属性)用不同的标签来映射。如果实体对象中的某个属性不需要存储在数据库中,那么在*.hbm.xml配置文件就不需要配置这个属性。
四、HibernateUtil工具类
我经常使用的一个HibernateUtil工具类代码如下所示:
public final class HibernateUtil {
private static SessionFactory sessionFactory;
private static ThreadLocal session = new ThreadLocal();
//防止外部创建新的HibernateUtil对象。其实是实现单例模式。
private HibernateUtil() {
}
//让此段代码只在java虚拟机加载此类时执行一次。因为这是一段相当耗时的操作。
static {
//读取hibernate.cfg.xml配置文件,如果你的配置文件不使用默认的hibernate.cfg.xml时,
//只需在cfg.configure("fileName")就可以了。fileName为你指定的hibernate配置文件的文件名。
// configure()方法的原代码如下 :
/**
* Use the mappings and properties specified in an application
* resource named <tt>hibernate.cfg.xml</tt>.
*/
//public Configuration configure() throws HibernateException {
//configure( "/hibernate.cfg.xml" );
//return this;
//}
Configuration cfg = new Configuration();
cfg.configure();
sessionFactory = cfg.buildSessionFactory();
}
public static Session getThreadLocalSession() {
Session s = (Session) session.get();
if (s == null) {
s = getSession();
session.set(s);
}
return s;
}
public static void closeSession() {
Session s = (Session) session.get();
if (s != null) {
s.close();
session.set(null);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
public static Session getSession() {
return sessionFactory.openSession();
}
//增加对象
public static void add(Object entity) {
Session s = null;
Transaction tx = null;
try {
s = HibernateUtil.getSession();
tx = s.beginTransaction();
s.save(entity);
tx.commit();
} catch(HibernateException e){
if(tx != null)
tx.rollback();
throw e;
}finally {
if (s != null)
s.close();
}
}
//更新对象
public static void update(Object entity) {
Session s = null;
Transaction tx = null;
try {
s = HibernateUtil.getSession();
tx = s.beginTransaction();
s.update(entity);
tx.commit();
} catch(HibernateException e){
if(tx != null)
tx.rollback();
throw e;
}finally {
if (s != null)
s.close();
}
}
//删除对象
public static void delete(Object entity) {
Session s = null;
Transaction tx = null;
try {
s = HibernateUtil.getSession();
tx = s.beginTransaction();
s.delete(entity);
tx.commit();
} catch(HibernateException e){
if(tx != null)
tx.rollback();
throw e;
}finally {
if (s != null)
s.close();
}
}
//获取对象
public static Object get(Class clazz, Serializable id) {
Session s = null;
try {
s = HibernateUtil.getSession();
Object obj = s.get(clazz, id);
return obj;
} finally {
if (s != null)
s.close();
}
}
}