欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

面向接口编程在hibernate中的应用 博客分类: hibernate 编程HibernateJavaBeanDAO 

程序员文章站 2024-03-16 10:06:04
...
在hibernate中通过操作对象来操作数据表,我们可以让POJO继承一个接口,
创建一个BaseDao,让Session对象保存,删除,更新一个接口实例,
在开启一个Session缓存之后,可以通过传入一个POJO对象来操作数据表



ObjectIn.java
----------------------
package com.supersit.hibernate.Interface;

public interface ObjectIn {

}






BaseDao.java
-----------------
package com.supersit.hibernate.dao;

import org.hibernate.Session;

import com.supersit.hibernate.exception.HibernateMsgException;

/**
 * 
 * <p>
 * 对于持久化类的添加,删除,修改可以通过继承这个类来实现 如果已开启Session缓存,可以传入这个Session对象进行相应的操作,当然也可以
 * 不调用此方法,直接调用Session对象相应的方法即可,在这里只是想实现面向接口编程
 * 对于没有开启Session缓存的情况,直接调用只有一个参数的方法,在方法体中会自动创建 Session对象,进行相关操作...
 * </p>
 * 
 * @author chenwei
 * 2007-10-26
 * 
 */

public class BaseDao {

	/**
	 * <p>
	 * 保存一个com.supersit.hibernate.Interface.ObjectIn对象
	 * </p>
	 * 
	 * @author chenwei
	 * @param com.supersit.hibernate.Interface.ObjectIn
	 * @return boolean
	 * @throws HibernateMsgException
	 */
	public boolean saveObj(com.supersit.hibernate.Interface.ObjectIn in)
			throws HibernateMsgException {

		org.hibernate.Session ses = null;
		org.hibernate.Transaction tran = null;
		boolean flag = true;
		try {
			ses = com.supersit.hibernate.factory.HibernateSessionFactory1
					.getSession();
			tran = ses.beginTransaction();
			ses.save(in);
			tran.commit();
		} catch (Exception e) {
			e.printStackTrace();
			flag = false;
			com.supersit.hibernate.factory.HibernateSessionFactory
					.rollbackTran(tran);
		} finally {
			com.supersit.hibernate.factory.HibernateSessionFactory
					.closeSession(ses);
		}
		return flag;
	}

	/**
	 * <p>
	 * 保存一个com.supersit.hibernate.Interface.ObjectIn对象
	 * </p>
	 * 
	 * @author chenwei
	 * @param com.supersit.hibernate.Interface.ObjectIn
	 * @return boolean
	 * @throws HibernateMsgException
	 */
	public boolean saveObj(Session session,
			com.supersit.hibernate.Interface.ObjectIn in) {
		boolean flag = true;
		try {
			session.save(in);
		} catch (Exception e) {
			e.printStackTrace();
			flag = false;
		}
		return flag;
	}

	/**
	 * <p>
	 * 修改一个com.supersit.hibernate.Interface.ObjectIn对象
	 * </p>
	 * 
	 * @author chenwei
	 * @param com.supersit.hibernate.Interface.ObjectIn
	 * @return boolean
	 * @throws HibernateMsgException
	 */
	public boolean updateObj(com.supersit.hibernate.Interface.ObjectIn in)
			throws HibernateMsgException {
		boolean flag = true;
		org.hibernate.Session ses = null;
		org.hibernate.Transaction tran = null;
		try {
			ses = com.supersit.hibernate.factory.HibernateSessionFactory
					.getSession();
			tran = ses.beginTransaction();
			ses.update(in);
			tran.commit();
		} catch (Exception e) {
			flag = false;
			com.supersit.hibernate.factory.HibernateSessionFactory
					.rollbackTran(tran);
		} finally {
			com.supersit.hibernate.factory.HibernateSessionFactory
					.closeSession(ses);
		}
		return flag;
	}

	/**
	 * <p>
	 * 修改一个com.supersit.hibernate.Interface.ObjectIn对象
	 * </p>
	 * 
	 * @author chenwei
	 * @param com.supersit.hibernate.Interface.ObjectIn
	 * @return boolean
	 * @throws HibernateMsgException
	 */
	public boolean updateObj(Session session,
			com.supersit.hibernate.Interface.ObjectIn in) {
		boolean flag = true;
		try {
			session.update(in);
		} catch (Exception e) {
			flag = false;
		}
		return flag;
	}

	/**
	 * <p>
	 * 删除一个com.supersit.hibernate.Interface.ObjectIn对象
	 * </p>
	 * 
	 * @author chenwei
	 * @param com.supersit.hibernate.Interface.ObjectIn
	 * @return boolean
	 * @throws HibernateMsgException
	 */

	public boolean deleteObj(com.supersit.hibernate.Interface.ObjectIn in)
			throws HibernateMsgException {
		boolean flag = true;
		org.hibernate.Session session = null;
		org.hibernate.Transaction tran = null;
		try {
			session = com.supersit.hibernate.factory.HibernateSessionFactory
					.getSession();
			tran = session.beginTransaction();
			session.delete(in);
			tran.commit();
		} catch (Exception e) {
			flag = false;
			com.supersit.hibernate.factory.HibernateSessionFactory
					.rollbackTran(tran);
		} finally {
			com.supersit.hibernate.factory.HibernateSessionFactory
					.closeSession(session);
		}
		return flag;
	}

	/**
	 * <p>
	 * 删除一个com.supersit.hibernate.Interface.ObjectIn对象
	 * </p>
	 * 
	 * @author chenwei
	 * @param com.supersit.hibernate.Interface.ObjectIn
	 * @return boolean
	 * @throws HibernateMsgException
	 */

	public boolean deleteObj(Session session,
			com.supersit.hibernate.Interface.ObjectIn in) {
		boolean flag = true;
		try {
			session.delete(in);
		} catch (Exception e) {
			flag = false;
		}
		return flag;
	}
}



Employee.java
---------------------------
package com.supersit.hibernate.bean;

import java.util.Date;
import java.util.Set;

public class Employee implements java.io.Serializable,
		com.supersit.hibernate.Interface.ObjectIn {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	
	//雇员编号
	private Integer eid;

	//雇员姓名
	private String ename;

	//入职时间
	private java.util.Date join_date;

	
	//角色集合
	private java.util.Set<Role> roles = new java.util.HashSet<Role>();

	public Employee(Integer eid, String ename, Date join_date) {
		this.eid = eid;
		this.ename = ename;
		this.join_date = join_date;
	}

	public Employee(Integer eid, String ename, Date join_date, Set<Role> roles) {
		this.eid = eid;
		this.ename = ename;
		this.join_date = join_date;
		this.roles = roles;
	}

	public java.util.Set getRoles() {
		return roles;
	}

	public void setRoles(java.util.Set<Role> roles) {
		this.roles = roles;
	}

	public Employee() {
	}

	public Integer getEid() {
		return eid;
	}

	public void setEid(Integer eid) {
		this.eid = eid;
	}

	public String getEname() {
		return ename;
	}

	public void setEname(String ename) {
		this.ename = ename;
	}

	public java.util.Date getJoin_date() {
		return join_date;
	}

	public void setJoin_date(java.util.Date join_date) {
		this.join_date = join_date;
	}

}




调和示例:
---------------
// 保存对象
	public static void insert() throws HibernateMsgException {

		java.util.Date date = new java.util.Date();
		java.text.SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		try {
			date = format.parse("2007-10-17");
		} catch (ParseException e) {
			e.printStackTrace();
		}

//创建一个雇员对象
		Employee emp = new Employee(new Integer(1), "张信哲", date);

		Session session = null;
		Transaction tran = null;
		try {
			session = com.supersit.hibernate.factory.HibernateSessionFactory
					.getSession();
			tran = session.beginTransaction();
//已开启Session缓存,调用有两个参数的保存方法,如果没有开启Session缓存,直接调用有一个参数的保存方法即可实现
			boolean b = new BaseDao().saveObj(session, emp);
			if (b) {
				System.out.println("成功保存一个Employee对象");
			}
			tran.commit();
		} catch (Exception e) {
			com.supersit.hibernate.factory.HibernateSessionFactory
					.rollbackTran(tran);
		} finally {
			com.supersit.hibernate.factory.HibernateSessionFactory
					.closeSession(session);
		}

	}

上一篇: 百度图像识别java使用笔记

下一篇: