CustomerDao.java
package cn.edu.zjut.dao;
import java.util.List;
import org.hibernate.Query;
import cn.edu.zjut.po.Customer;
public class CustomerDao extends BaseHibernateDao{
public CustomerDao() {
// TODO Auto-generated constructor stub
}
//根据hql语句查找
public List findByHql(String hql) {
try {
String queryString = hql;
Query queryObject = getSession().createQuery(queryString);
return queryObject.list();
}catch (RuntimeException re) {
//log.error("find by hql failed", re);
throw re;
}
}
//查找全部,当然其实这个方法是不需要的,因为只要有了用hql语句查找,在service方法中就可以让hql语句为"from 实体类名"
public List findAll() {
try {
String queryString = "from Customer";//这里是from 实体类名 而不是数据表名,这句话代表查询全部
Query queryObject = getSession().createQuery(queryString);
return queryObject.list();
}catch(RuntimeException re){
throw re;
}finally {
session.close();
}
}
//增加一条记录
public void save(Customer customer) {
try {
getSession().save(customer);
}catch(RuntimeException re) {
throw re;
}
}
//更新一条记录
public void update(Customer customer) {
try {
getSession().update(customer);
getSession().flush();
}catch(RuntimeException re) {
throw re;
}finally {
getSession().close();
}
}
//删除一条记录
public void delete(Customer customer) {
try {
getSession().delete(customer);
getSession().flush();
}catch(RuntimeException re) {
throw re;
}finally {
getSession().close();
}
}
}
这里不再需要在每个方法中加入:
Configuration cfg= new Configuration() .configure();
ServiceRegistry sr = new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry();
SessionFactory sf = cfg.buildSessionFactory(sr);
Session session = sf.openSession();
因为它继承了BaseHibernateDao,可以直接使用getSession()来获取session,而在BaseHibernateDao的getSession方法中又是用来HibernateUtil。这两个.java文件详见 我的另一篇博客
注意事务的使用。
解释一下下面的saveOrUpdate方法,是在中间表的时候让它自己判断是要save还是update
package com.searchSub.dao;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.searchSub.po.Customer;
public class CustomerDAO extends BaseHibernateDao implements ICustomerDAO {
public void save(Customer transientInstance) {
System.out.println("execute --save()-- method.");
Transaction tran = null;
Session session = null;
try {
session = getSession();
tran = session.beginTransaction();
session.save(transientInstance);
tran.commit();
} catch (RuntimeException re) {
if (tran != null)
tran.rollback();
throw re;
} finally {
session.close();
}
}
public void delete(Customer transientInstance) {
System.out.println("execute --delete()-- method.");
Transaction tran = null;
Session session = null;
try {
session = getSession();
tran = session.beginTransaction();
session.delete(transientInstance);
tran.commit();
} catch (RuntimeException re) {
if (tran != null)
tran.rollback();
throw re;
} finally {
session.close();
}
}
public void update(Customer transientInstance) {
System.out.println("execute --update()-- method.");
Transaction tran = null;
Session session = null;
try {
session = getSession();
tran = session.beginTransaction();
session.merge(transientInstance);
tran.commit();
} catch (RuntimeException re) {
if (tran != null)
tran.rollback();
throw re;
} finally {
session.close();
}
}
public List findByHQL(String hql) {
System.out.println("execute --findByHQL()-- method.");
Transaction tran = null;
Session session = null;
try {
session = getSession();
tran = session.beginTransaction();
String queryString = hql;
Query queryObject = getSession().createQuery(queryString);
tran.commit();
return queryObject.list();
} catch (RuntimeException re) {
if (tran != null)
tran.rollback();
throw re;
} finally {
session.close();
}
}
public Customer findById(int id) {
System.out.println("execute --findById()-- method.");
Customer cust = null;
Transaction tran = null;
Session session = null;
try {
session=getSession();
tran = session.beginTransaction();
cust = (Customer) session.get(Customer.class, id);
tran.commit();
}catch (RuntimeException re) {
if (tran != null)
tran.rollback();
throw re;
}finally {
session.close();
}
return cust;
}
public void saveOrUpdate(Customer transientInstance) {
System.out.println("execute --save()-- method.");
Transaction tran = null;
Session session = null;
try {
session = getSession();
tran = session.beginTransaction();
session.saveOrUpdate(transientInstance);
tran.commit();
} catch (RuntimeException re) {
if (tran != null)
tran.rollback();
throw re;
} finally {
session.close();
}
}
}
本文详细介绍了CustomerDao类的实现,包括使用Hibernate进行客户数据的增删改查操作,通过HQL语句查找特定数据,以及如何利用saveOrUpdate方法自动判断并执行保存或更新操作。
1742

被折叠的 条评论
为什么被折叠?



