前几天用eclipse下生成的Hibernate DAO做了一个实现的增删查改的小例子,在这里解释下生成DAO中的几个方法到底是做什么用的.
这里我将以注释的形式在下面的这段java代码中解释.
packagedao;
/***//**
*很简单引入你要用的包
*/
importjava.util.List;
importorg.apache.commons.logging.Log;
importorg.apache.commons.logging.LogFactory;
importorg.hibernate.LockMode;
importorg.hibernate.Query;
importorg.hibernate.criterion.Example;

/***//**
*类UsertableDAO继承了BaseHibernateDAO
*/
publicclassUsertableDAOextendsBaseHibernateDAO...{
privatestaticfinalLoglog=LogFactory.getLog(UsertableDAO.class);
publicstaticfinalStringNAME="name";
publicstaticfinalStringAGE="age";
/***//**
*save()方法提供了向数据库中添加数据的功能,但只能添加,这个DAO没有生成Update()的方法
*但你可以简单的八save()方法改称具有Update功能:将getSession().save
*(transientInstance);这句改成
*getSession().merge(transientInstance);或者getSession().saveOrUpdate
*(transientInstance);
*/
publicvoidsave(UsertabletransientInstance)...{
log.debug("savingUsertableinstance");
try...{
getSession().save(transientInstance);
log.debug("savesuccessful");
}catch(RuntimeExceptionre)...{
log.error("savefailed",re);
throwre;
}
}
/***//**
*delete()方法看名知意是用来删除的.
*/
publicvoiddelete(UsertablepersistentInstance)...{
log.debug("deletingUsertableinstance");
try...{
getSession().delete(persistentInstance);
log.debug("deletesuccessful");
}catch(RuntimeExceptionre)...{
log.error("deletefailed",re);
throwre;
}
}
/***//**
*findById()方法实现了按ID查询数据.
*/
publicUsertablefindById(java.lang.Integerid)...{
log.debug("gettingUsertableinstancewithid:"+id);
try...{
Usertableinstance=(Usertable)getSession().get("dao.Usertable",
id);
returninstance;
}catch(RuntimeExceptionre)...{
log.error("getfailed",re);
throwre;
}
}
/***//**
*findByExample()方法实现的功能相当于"select*fromUsertable"实现的功能就是查询所有
*数据.
*/
publicListfindByExample(Usertableinstance)...{
log.debug("findingUsertableinstancebyexample");
try...{
Listresults=getSession().createCriteria("dao.Usertable").add(
Example.create(instance)).list();
log.debug("findbyexamplesuccessful,resultsize:"
+results.size());
returnresults;
}catch(RuntimeExceptionre)...{
log.error("findbyexamplefailed",re);
throwre;
}
}
/***//**
*findByProperty()方法用来灵活的提供一种按条件查询的方法,你可以自己定义要按什么样的方
*式查询.
*/
publicListfindByProperty(StringpropertyName,Objectvalue)...{
log.debug("findingUsertableinstancewithproperty:"+propertyName
+",value:"+value);
try...{
StringqueryString="fromUsertableasmodelwheremodel."
+propertyName+"=?";
QueryqueryObject=getSession().createQuery(queryString);
queryObject.setParameter(0,value);
returnqueryObject.list();
}catch(RuntimeExceptionre)...{
log.error("findbypropertynamefailed",re);
throwre;
}
}
/***//**
*findByName()和findByAge()方法就是调用了findByProperty()方法的实现按名字和年龄查询
*的功能
*/
publicListfindByName(Objectname)...{
returnfindByProperty(NAME,name);
}

publicListfindByAge(Objectage)...{
returnfindByProperty(AGE,age);
}
}
关于merge()attachDirty()attachClean()三种方法下面做一个简单的介绍

/***//**
*将传入的detached状态的对象的属性复制到持久化对象中,并返回该持久化对象
*如果该session中没有关联的持久化对象,加载一个,如果传入对象未保存,保存一个副本并作为持久对象返回,传入对象依然保持detached状态。
*@seecom.CodeDepts
*/
publicCodeDeptsmerge(CodeDeptsdetachedInstance)...{
log.debug("mergingCodeDeptsinstance");
try...{
CodeDeptsresult=(CodeDepts)getSession()
.merge(detachedInstance);
log.debug("mergesuccessful");
returnresult;
}catch(RuntimeExceptionre)...{
log.error("mergefailed",re);
throwre;
}
}

/***//**
*将传入的对象持久化并保存。
*如果对象未保存(Transient状态),调用save方法保存。如果对象已保存(Detached状态),调用update方法将对象与Session重新关联。
*@seecom.CodeDepts
*/

publicvoidattachDirty(CodeDeptsinstance)...{
log.debug("attachingdirtyCodeDeptsinstance");
try...{
getSession().saveOrUpdate(instance);
log.debug("attachsuccessful");
}catch(RuntimeExceptionre)...{
log.error("attachfailed",re);
throwre;
}
}



/***//**
*将传入的对象状态设置为Transient状态
*@seecom.CodeDepts
*/

publicvoidattachClean(CodeDeptsinstance)...{
log.debug("attachingcleanCodeDeptsinstance");
try...{
getSession().lock(instance,LockMode.NONE);
log.debug("attachsuccessful");
}catch(RuntimeExceptionre)...{
log.error("attachfailed",re);
throwre;
}
}
好了解析到此为止如果你还不明白的话给我留言我一定尽我的所能去回答你的问题!
本文通过一个使用Eclipse生成的Hibernate DAO实例,详细解析了增删查改等核心方法的实现原理及使用方式,包括save(), delete(), findById(), findByExample(), findByProperty()等。
216

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



