首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2EE开发 >

还是关于通用泛型DAO的理解和实现,该如何解决

2012-01-28 
还是关于通用泛型DAO的理解和实现前段时间,在javaeye中关于这个的讨论内容很多,正好我在优化我的dao基类,

还是关于通用泛型DAO的理解和实现
前段时间,在javaeye中关于这个的讨论内容很多,正好我在优化我的dao基类,想把dao做成通用的,所以借鉴了大家的思想和代码.

一直在潜水,很不好意思,决定把我的实现发在这里,贡献给大家,有什么不妥的欢迎指正.

关于通用dao的优缺点这里不谈,只说实现

现在还有两个问题很头疼,先看代码
[code=java]
package org.sigon.framework.dao;

import

/**
* dao基类
*
* @author sigon
*
*/
public class BaseDao extends
HibernateDaoSupport {

private Class poClass = null;
private Class voClass = null;

public BaseDao(Class poClass, Class voClass){
this.poClass = poClass;
this.voClass = voClass;
}
/**
* 存储po对象
*
* @param entity
* @throws Exception
*/
protected void save(PO entity) throws Exception {
this.getHibernateTemplate().save(entity);
}

public void saveUseVO(VO vo) throws Exception{
PO po = getPOClass().newInstance();
BeanUtils.copyProperties(vo, po);
save(po);
}
protected void saveOrUpdate(PO entity) throws Exception {
this.getHibernateTemplate().saveOrUpdate(entity);
}

/**
* 删除po
*/
protected void delete(Serializable id) throws Exception {
PO entity = get(id);
this.getHibernateTemplate().delete(entity);
}

/**
* 更新po
*/
protected void update(VO vo) throws Exception {
PO entity = getPOClass().newInstance();
BeanUtils.copyProperties(vo, entity);
this.getHibernateTemplate().update(entity);
}

/**
* 取出po并且使对象脱离缓存
*/
protected PO getAndEvict(Serializable id)
throws Exception {
PO result = get(id);
this.getHibernateTemplate().evict(result);
return result;
}

/**
* 按id取对象
*/
@SuppressWarnings("unchecked")
protected PO get(Serializable id)
throws Exception {
return (PO)this.getHibernateTemplate().get(getPOClass(), id);
}

public VO getVO(Serializable id) throws Exception{
PO po = get(id);
return convertPO2VO(po);
}

//
/**
* 取出po列表,按hql
*/
@SuppressWarnings("unchecked")
protected List find(String hql) throws Exception {
return getHibernateTemplate().find(hql);
}

/**
* 执行带参数的hql
*/
@SuppressWarnings("unchecked")
protected List find(String hql, Object... params) throws Exception {
return getHibernateTemplate().find(hql, params);
}

/**
* qbc式查询list
*/
@SuppressWarnings("unchecked")
protected List findListByQBC(DetachedCriteria criteria)
throws Exception {
return getHibernateTemplate().findByCriteria(criteria);
}
//
///**
// * qbc式查询对象
// */
//@SuppressWarnings("unchecked")
//protected PO findObjectByQBC(final DetachedCriteria criteria)
//throws Exception {
//return (PO) getHibernateTemplate().execute(new HibernateCallback() {
//public Object doInHibernate(Session session)
//throws HibernateException, SQLException {
//return criteria.getExecutableCriteria(session).uniqueResult();
//}
//}, true);
//}

/**
* 根据属性值查找
*
* @param entityClass
* @param propertyName
* @param value
* @return
*/
@SuppressWarnings("unchecked")
public List findByProperty(String propertyName,
Object value) throws Exception{
List poList = createCriteria(Restrictions.eq(propertyName, value)).list();
return convertListPO2VO(poList);
}
/**
* 按属性查找唯一对象.
*/
@SuppressWarnings("unchecked")


public VO findUniqueByProperty(String propertyName, Object value) throws Exception{
PO po = (PO)createCriteria(Restrictions.eq(propertyName, value)).uniqueResult();
return convertPO2VO(po);
}
/**
* 根据Criterion条件创建Criteria,后续可进行更多处理,辅助函数.
*/
public Criteria createCriteria(Criterion... criterions) {
Criteria criteria = getSession().createCriteria(poClass);
for (Criterion c : criterions) {
criteria.add(c);
}
return criteria;
}
/**
* 用sql查询,结果为vo形式
*
* @param hql
* @param args
* @param clazz
* @throws Exception
*/
@SuppressWarnings("unchecked")
protected List queryListUseSQL(String sql, Object[] args, Class clazz)
throws Exception {
return (List) this.getHibernateTemplate().execute(
new VoListCallBack(sql, args, clazz));
}

/**
* 用hql分页查找
*
* @param hql
* @param args
* @param page
* @throws Exception
*/
@SuppressWarnings("unchecked")
protected void findPageByHQL(String hql, Object[] args, Page page)
throws Exception {
getHibernateTemplate().execute(new PageCallback(hql, args, page));
page.setList(convertListPO2VO(page.getList()));
}

/**
* 用sql分页查找,返回vo形式
*
* @param sql
* @param args
* @param page
* @param clazz
* @return
* @throws Exception
*/
@SuppressWarnings("unchecked")
protected Page findPageBySQL(String sql, Object[] args, Page page) throws Exception {
page.setTotal((Integer) getHibernateTemplate().execute(
new CountCallBack(sql)));
Integer startIndex = (page.getCurrent() - 1) * page.getSize();
String newSql = Props.formatProperty("jdbc.limit.format", new Object[] {
startIndex, page.getSize() });
newSql = TextUtil.parseString(newSql, new Object[] { sql });
List list = (List) getHibernateTemplate().execute(
new VoListCallBack(newSql, args, getVOClass()));
page.setList(list);
return page;
}
/**
* 通过count查询获得本次查询所能获得的对象总数.
* @return page对象中的totalCount属性将赋值.
* @throws IllegalAccessException
* @throws NoSuchFieldException
* @throws IllegalArgumentException
*/
@SuppressWarnings("unchecked")
protected int countQueryResult(Criteria c) throws IllegalArgumentException, NoSuchFieldException, IllegalAccessException {
CriteriaImpl impl = (CriteriaImpl) c;

// 先把Projection、ResultTransformer、OrderBy取出来,清空三者后再执行Count操作
Projection projection = impl.getProjection();
ResultTransformer transformer = impl.getResultTransformer();

List orderEntries = null;
orderEntries = (List) BeanUtil.getFieldValue(impl, "orderEntries");
BeanUtil.setFieldValue(impl, "orderEntries", new ArrayList());

// 执行Count查询
int totalCount = (Integer) c.setProjection(Projections.rowCount()).uniqueResult();
// 将之前的Projection和OrderBy条件重新设回去
c.setProjection(projection);
if (projection == null) {
c.setResultTransformer(CriteriaSpecification.ROOT_ENTITY);
}
if (transformer != null) {
c.setResultTransformer(transformer);
}
BeanUtil.setFieldValue(impl, "orderEntries", orderEntries);

return totalCount;
}
/**
* 返回泛型po的class
* @return
*/
@SuppressWarnings("unchecked")
private Class getPOClass() {
//if (this.poClass == null) {
//this.poClass = (Class) ((ParameterizedType) getClass()
//.getGenericSuperclass()).getActualTypeArguments()[0];


//}
return this.poClass;
}
/**
* 返回泛型vo的class
* @return
*/
@SuppressWarnings("unchecked")
private Class getVOClass() {
//if (this.voClass == null) {
//this.voClass = (Class) ((ParameterizedType) getClass()
//.getGenericSuperclass()).getActualTypeArguments()[1];
//}
return this.voClass;
}
/**
* 将po列表转换为vo列表
* @param poList
* @return
* @throws Exception
*/
private List convertListPO2VO(List poList) throws Exception{
List voList = null;
if(poList.size() > 0){
voList = new ArrayList(poList.size());
PO po = null;
VO vo = null;
for(int i = 0 ; i < poList.size() ; i ++){
po = poList.get(i);
vo = convertPO2VO(po);
voList.add(vo);
}
}
return voList;
}
private VO convertPO2VO(PO po) throws Exception{
VO vo = getVOClass().newInstance();
BeanUtils.copyProperties(po, vo);
return vo;
}
}
[/code]
具体问题请看一楼

[解决办法]
BaseDao userDao = new BaseDao<UserPO, UserVO>(){};这样试试看
[解决办法]
似乎都是这样,要么传entity要么就是继承,要么用刚才的inner还是需要个factory.似乎没有更好的解决方案
[解决办法]
没必要自己折腾了,看看appfuse怎么做的吧
[解决办法]
看看官方的教学吧

http://www.hibernate.org/328.html
[解决办法]

热点排行