1.Hibernate实例状态
2.HIbernate初始化类
session对象是Hibernate中数据库持久化操作的核心,负责Hibernate所有持久化操作。session对象通过SessionFactory对象获取的。
Configuration cfg=new Configuration().configure(); //加载Hibernate配置文件
factory=cfg.buildSessionFactory();//实例化SessionFactory
以下HIbernate初始类,就可以有效管理Session,避免了Session的多线程共享数据的问题。
package com.mr.hibernate;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
/**
* Hibernate初始化类
*
*/
public class HibernateInitialize {
private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();//ThreadLocal对象
private static SessionFactory sessionFactory = null;//SessionFactory对象
//静态块
static {
try {
// 加载Hibernate配置文件
Configuration cfg = new Configuration().configure();
sessionFactory = cfg.buildSessionFactory();
} catch (Exception e) {
System.err.println("创建会话工厂失败");
e.printStackTrace();
}
}
/**
* 获取Session
* @return Session
* @throws HibernateException
*/
public static Session getSession() throws HibernateException {
Session session = (Session) threadLocal.get();
if (session == null || !session.isOpen()) {
if (sessionFactory == null) {
rebuildSessionFactory();
}
session = (sessionFactory != null) ? sessionFactory.openSession(): null;
threadLocal.set(session);
}
return session;
}
/**
* 重建会话工厂
*/
public static void rebuildSessionFactory() {
try {
// 加载Hibernate配置文件
Configuration cfg = new Configuration().configure();
sessionFactory = cfg.buildSessionFactory();
} catch (Exception e) {
System.err.println("创建会话工厂失败");
e.printStackTrace();
}
}
/**
* 获取SessionFactory对象
* @return SessionFactory对象
*/
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
/**
* 关闭Session
* @throws HibernateException
*/
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null);//
if (session != null) {
session.close();//关闭Session
}
}
}
以下类处理保存、查询、删除、更新
package com.mr.main;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.mr.hibernate.HibernateInitialize;
import com.mr.product.Product;
public class AddProduct {
/**
* 添加商品
*/
public static void main(String[] args) {
Session session=null;
Product product=new Product();
product.setFactory("北京大学");
product.setName("数据挖掘");
product.setPrice(45.88);
product.setRemark("56565");
//Hibernate持久化操作
try {
session=HibernateInitialize.getSession();//获取session
session.beginTransaction();//开启事务
session.save(product);//执行数据库添加操作
session.getTransaction().commit();//事务提交
} catch (HibernateException e) {
// TODO Auto-generated catch block
session.getTransaction().rollback();//事务回滚
System.out.println("数据添加失败");
e.printStackTrace();
}finally{
HibernateInitialize.closeSession();
}
}
//查询商品
// public static void main(String[] args) {
// Session session=null;
// //Hibernate的持久化操作
// try {
// session=HibernateInitialize.getSession();
// //装载对象
Product product=(Product) session.get(Product.class, 12);
// Product product=(Product) session.load(Product.class, 4);
// System.out.println("输出的信息是"+product.getId());
// } catch (HibernateException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }finally{
// HibernateInitialize.closeSession();
// }
//
// }
//删除商品
// public static void main(String[] args) {
// Session session=null;
// try {
// session=HibernateInitialize.getSession();//获取session
// Transaction tx1 = session.beginTransaction();
// Product product=(Product) session.get(Product.class, new Integer(4));//装载对象
Product product=(Product) session.get(Product.class, new String("明日科技"));//装载对象
// session.delete(product);//删除持久化对象
// tx1.commit();// 清理缓存,执行delete语句
session.flush();// 强制刷新提交
// } catch (HibernateException e) {
// // TODO Auto-generated catch block
// System.out.println("对象删除失败!");
// e.printStackTrace();
// }finally{
// HibernateInitialize.closeSession();
// }
// }
//修改数据
// public static void main(String[] args) {
// Session session=null;
// try{
// session=HibernateInitialize.getSession();//获取session
// Transaction tx1=session.beginTransaction();
// Product product=(Product) session.get(Product.class, new Integer(6));//装载对象
// product.setRemark("2222");
session.flush();// 强制刷新提交
// tx1.commit();
// }catch(HibernateException e) {
// System.out.println("对象修改失败!");
// e.printStackTrace();
// }finally{
// HibernateInitialize.closeSession();
// }
// }
}