HibernateUtil.java
资源名称:(J2EE)oa.rar [点击查看]
上传用户:lm2018
上传日期:2015-12-12
资源大小:30449k
文件大小:1k
源码类别:
Jsp/Servlet
开发平台:
Java
- package com.oa.module.pub.ectomere;
- import java.io.Serializable;
- import org.hibernate.Session;
- import org.hibernate.SessionFactory;
- import org.hibernate.Transaction;
- public class HibernateUtil {
- private SessionFactory sf;
- Session session = null;
- Transaction tx = null;
- public boolean save(Object object){
- try {
- session = sf.openSession();
- tx = session.beginTransaction();
- session.save(object);
- tx.commit();
- session.flush();
- return true;
- } catch (Exception e) {
- e.printStackTrace();
- tx.rollback();
- }finally{
- session.close();
- sf.close();
- }
- return false;
- }
- public Object get(Class poclass,Serializable ino){
- Object object = null;
- Session session = null;
- try {
- session = sf.openSession();
- object =session.get(poclass,ino);
- session.flush();
- } catch (Exception e) {
- e.printStackTrace();
- }finally{
- session.close();
- sf.close();
- }
- return object;
- }
- public boolean update(Object object){
- Session session = null;
- Transaction tx = null;
- try {
- session = sf.openSession();
- tx = session.beginTransaction();
- session.update(object);
- tx.commit();
- session.flush();
- return true;
- } catch (Exception e) {
- e.printStackTrace();
- tx.rollback();
- }finally{
- session.close();
- sf.close();
- }
- return false;
- }
- public SessionFactory getSf() {
- return sf;
- }
- public void setSf(SessionFactory sf) {
- this.sf = sf;
- }
- }