前几天用eclipse下生成的Hibernate DAO做了一个实现的增删查改的小例子,在这里解释下生成DAO中的几个方法到底是做什么用的.
这里我将以注释的形式在下面的这段java代码中解释.
package dao;
/** *//**
* 很简单引入你要用的包
*/
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;
/** *//**
* 类UsertableDAO继承了BaseHibernateDAO
*/
public class UsertableDAO extends BaseHibernateDAO ...{
private static final Log log = LogFactory.getLog(UsertableDAO.class);
public static final String NAME = "name";
public static final String AGE = "age";
/** *//**
* save()方法提供了向数据库中添加数据的功能,但只能添加,这个DAO没有生成Update()的方法
* 但你可以简单的八save()方法改称具有Update功能:将getSession().save
* (transientInstance);这句改成
* getSession().merge(transientInstance);或者getSession().saveOrUpdate
* (transientInstance);
*/
public void save(Usertable transientInstance) ...{
log.debug("saving Usertable instance");
try ...{
getSession().save(transientInstance);
log.debug("save successful");
} catch (RuntimeException re) ...{
log.error("save failed", re);
throw re;
}
}
/** *//**
* delete()方法看名知意是用来删除的.
*/
public void delete(Usertable persistentInstance) ...{
log.debug("deleting Usertable instance");
try ...{
getSession().delete(persistentInstance);
log.debug("delete successful");
} catch (RuntimeException re) ...{
log.error("delete failed", re);
throw re;
}
}
/** *//**
* findById()方法实现了按ID查询数据.
*/
public Usertable findById(java.lang.Integer id) ...{
log.debug("getting Usertable instance with id: " + id);
try ...{
Usertable instance = (Usertable) getSession().get("dao.Usertable",
id);
return instance;
} catch (RuntimeException re) ...{
log.error("get failed", re);
throw re;
}
}
/** *//**
* findByExample()方法实现的功能相当于"select * from Usertable"实现的功能就是查询所有
* 数据.
*/
public List findByExample(Usertable instance) ...{
log.debug("finding Usertable instance by example");
try ...{
List results = getSession().createCriteria("dao.Usertable").add(
Example.create(instance)).list();
log.debug("find by example successful, result size: "
+ results.size());
return results;
} catch (RuntimeException re) ...{
log.error("find by example failed", re);
throw re;
}
}
/** *//**
* findByProperty()方法用来灵活的提供一种按条件查询的方法,你可以自己定义要按什么样的方
* 式查询.
*/
public List findByProperty(String propertyName, Object value) ...{
log.debug("finding Usertable instance with property: " + propertyName
+ ", value: " + value);
try ...{
String queryString = "from Usertable as model where model."
+ propertyName + "= ?";
Query queryObject = getSession().createQuery(queryString);
queryObject.setParameter(0, value);
return queryObject.list();
} catch (RuntimeException re) ...{
log.error("find by property name failed", re);
throw re;
}
}
/** *//**
* findByName()和findByAge()方法就是调用了findByProperty()方法的实现按名字和年龄查询
* 的功能
*/
public List findByName(Object name) ...{
return findByProperty(NAME, name);
}
public List findByAge(Object age) ...{
return findByProperty(AGE, age);
}
}
关于merge() attachDirty() attachClean()三种方法下面做一个简单的介绍
/** *//**
* 将传入的detached状态的对象的属性复制到持久化对象中,并返回该持久化对象
* 如果该session中没有关联的持久化对象,加载一个,如果传入对象未保存,保存一个副本并作为持久对象返回,传入对象依然保持detached状态。
* @see com.CodeDepts
*/
public CodeDepts merge(CodeDepts detachedInstance) ...{
log.debug("merging CodeDepts instance");
try ...{
CodeDepts result = (CodeDepts) getSession()
.merge(detachedInstance);
log.debug("merge successful");
return result;
} catch (RuntimeException re) ...{
log.error("merge failed", re);
throw re;
}
}
/** *//**
* 将传入的对象持久化并保存。
* 如果对象未保存(Transient状态),调用save方法保存。如果对象已保存(Detached状态),调用update方法将对象与Session重新关联。
* @see com.CodeDepts
*/
分享到:
相关推荐
hibernate eclipse插件生成dao样例
逆向生成hibernate配置文件逆向生成hibernate配置文件逆向生成hibernate配置文件逆向生成hibernate配置文件逆向生成hibernate配置文件逆向生成hibernate配置文件
Eclipse上使用Hibernate synanize Eclipse上使用Hibernate synanize Eclipse上使用Hibernate Eclipse上使用Hibernate
生成简单的dao层和seriver,用于三大框架 Struts2+spring+hibernate+dwr
eclipse从数据库逆向生成Hibernate实体类
自动生成dao 配置文件 自动生成dao的eclipse插件autoDAO
在Eclipse中使用Hibernate.pdf
Eclipse快速上手Hibernate
离线状态下或者联网状态下,在Eclipse中安装Hibernate Tools。 离线状态下或者联网状态下,在Eclipse中安装Hibernate Tools。
将文件解压后,将文件夹中的3个文件夹放到eclipse目录下的plugin下,然后重启eclipse就OK了
在eclipse中使用hibernate的技巧
Eclipse 在线配置 Hibernate Tools
精通Java EE项目案例-基于Eclipse Spring Struts Hibernate光盘源码.rar精通Java EE项目案例-基于Eclipse Spring Struts Hibernate光盘源码.rar精通Java EE项目案例-基于Eclipse Spring Struts Hibernate光盘源码....
北大青鸟 Eclipse搭建hibernate项目环境
NULL 博文链接:https://mazilaidaige.iteye.com/blog/1416042
本资源详细描述了Eclipse中反向生成hibernate实体类+jpa注释的详细过程,是一个不错的图文教程。
使用eclipse直接生成so文件.使用eclipse直接生成so文件
通过比较详细的步骤介绍了Eclipse中配置和使用Hibernate框架,并有一个简单例子实现,容易上手!
Eclipse快速上手Hibernate之入门实例
Eclipse使用hibernate插件反向生成数据库实体类,亲身操作,绝对真实有效