package com.xiva.hibernate.dao; import java.io.Serializable; public interface BaseDao<E extends Serializable, PK extends Serializable> { /** * 插入一个对象 * @param person */ public void insert(E entity); /** * 删除一个对象 * @param object */ public void delete(E entity); /** * 根据主键,使用到Criteria接口查询 * @param id * @return */ public E getByPK(PK id); /** * 直接使用session的get方法 * @param id * @return */ public E getById(PK id); /** * 更新一个对象 * @param person */ public void update(E entity); }
接口实现类:
package com.xiva.hibernate.dao; import java.io.Serializable; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import org.hibernate.Criteria; import org.hibernate.Session; import org.hibernate.Transaction; import org.hibernate.criterion.Restrictions; import com.xiva.hibernate.HibernateUtil; public class BaseDaoImpl<E extends Serializable, PK extends Serializable> implements BaseDao<E, PK> { private Class<?> entityClass; public BaseDaoImpl(){ Class<?> c = this.getClass(); Type type = c.getGenericSuperclass(); if (type instanceof ParameterizedType) { this.entityClass = (Class<?>) ((ParameterizedType) type) .getActualTypeArguments()[0]; } } /** * 插入数据库同样需要使用到事务 */ public void insert(E entity) { Session session = null; Transaction tx = null; try{ session = HibernateUtil.getSession(); tx = session.beginTransaction(); session.save(entity); tx.commit(); }finally{ if(session!=null){ session.close(); } } } /** * 删除一个对象与查询不同必须使用事务提交 */ public void delete(E entity) { Session session = null; Transaction tx = null; try{ session = HibernateUtil.getSession(); tx = session.beginTransaction(); session.delete(entity); tx.commit(); }finally{ if(session!=null){ session.close(); } } } @SuppressWarnings("unchecked") public E getByPK(PK id) { Session session = null; E entity = null; try { session = HibernateUtil.getSession(); Criteria criteria = session.createCriteria(this.entityClass); criteria.add(Restrictions.eq("id", id)); // List<Person> list = criteria.list(); // if(list.size()!=0) // person = list.get(0); entity = (E) criteria.uniqueResult(); // if(person!=null) // System.out.println(person.getDepartment().getDepartmentName()); } finally { if(session!=null){ session.close(); } } return entity; } public void update(E entity) { Session session = null; Transaction tx = null; try{ session = HibernateUtil.getSession(); tx = session.beginTransaction(); session.update(entity); tx.commit(); }finally{ if(session!=null){ session.close(); } } } @SuppressWarnings("unchecked") public E getById(PK id) { Session session = null; E entity = null; try { session = HibernateUtil.getSession(); entity = (E) session.get(this.entityClass, id); // Hibernate.initialize(person.getDepartment()); } finally { if(session!=null){ session.close(); } } return entity; } }
下面是调用方式:
package com.xiva.hibernate.dao; import com.xiva.hibernate.domain.Person; public interface PersonDao extends BaseDao<Person, Long>{ }
package com.xiva.hibernate.dao; import com.xiva.hibernate.domain.Person; public class PersonDaoImpl extends BaseDaoImpl<Person, Long> implements PersonDao { }
实体类:
package com.xiva.hibernate.domain; import java.io.Serializable; import java.util.Date; public class Person implements Serializable{ /** * */ private static final long serialVersionUID = -6190737414531892695L; private Long id; private String name; private String password; private Date birthday; private Department department; private IdCard idCard; private Address address; public Person() { } public Person(Long id, String name, String password, Department department, IdCard idCard, Date birthday, Address address) { this.id = id; this.name = name; this.password = password; this.department = department; this.idCard = idCard; this.birthday = birthday; this.address = address; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Department getDepartment() { return department; } public void setDepartment(Department department) { this.department = department; } public IdCard getIdCard() { return idCard; } public void setIdCard(IdCard idCard) { this.idCard = idCard; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } }
主要是需要实现Serializable接口。还有在实现类初始化的时候,需要知道是哪一个类,
public BaseDaoImpl(){ Class<?> c = this.getClass(); Type type = c.getGenericSuperclass(); if (type instanceof ParameterizedType) { this.entityClass = (Class<?>) ((ParameterizedType) type) .getActualTypeArguments()[0]; } }
也许上面的方法是实现了基本的Dao,但是不可取,这个只是使用hibernate框架,我们的事务只能在Dao层进行控制,而一般的数据回滚,我们是在业务逻辑层来做的;因此可能结合Spring来实现这样的功能比较简单,以后贴出例子。