Eclipse下生成HibernateDAO中的几个方法

开发 后端
本文介绍了Eclipse下生成HibernateDAO中的几个方法,如save()方法,delete()方法,findByExample()方法等。

* save()方法提供了向数据库中添加数据的功能,但只能添加,这个DAO没有生成Update()的方法
* 但你可以简单的把save()方法改称具有Update功能:将getSession().save * (transientInstance);这句改成
* getSession().merge(transientInstance);或者getSession().saveOrUpdate
*   (transientInstance);

  1. public void save(User transientInstance) {  
  2.    log.debug("saving User instance");  
  3.    try {  
  4.     Session session=getSession();  
  5.     Transaction tx=session.beginTransaction();  
  6.     session.save(transientInstance);  
  7.     tx.commit();  
  8.     session.close();  
  9.     log.debug("save successful");  
  10.    } catch (RuntimeException re) {  
  11.     log.error("save failed", re);  
  12.     throw re;  
  13.    }  

delete()方法用来删除的 实际上我们会用下边的这个方法进行删除

  1. public void delete(Integer id){  
  2.    log.debug("deleting User instance...");  
  3.    User user=findById(id);  
  4.    delete(user);  
  5. }  
  6.  
  7. public void delete(User persistentInstance) {  
  8.    log.debug("deleting User instance");  
  9.    try {  
  10.     Session session=getSession();  
  11.     Transaction tx=session.beginTransaction();  
  12.     session.delete(persistentInstance);  
  13.     tx.commit();  
  14.     session.close();  
  15.     log.debug("delete successful");  
  16.    } catch (RuntimeException re) {  
  17.     log.error("delete failed", re);  
  18.     throw re;  
  19.    }  
  20. }  

根据编号进行查找

  1. public User findById(java.lang.Integer id) {  
  2.    log.debug("getting User instance with id: " + id);  
  3.    try {  
  4.     User instance = (User) getSession().get("hbm.User", id);  
  5.     return instance;  
  6.    } catch (RuntimeException re) {  
  7.     log.error("get failed", re);  
  8.     throw re;  
  9.    }  

findByExample()方法实现的功能相当于"select * from Usertable"实现的功能就是查询所有 数据.

  1. public List findByExample(User instance) {  
  2.    log.debug("finding User instance by example");  
  3.    try {  
  4.     List results = getSession().createCriteria("hbm.User").add(  
  5.       Example.create(instance)).list();  
  6.     log.debug("find by example successful, result size: " 
  7.       + results.size());  
  8.     return results;  
  9.    } catch (RuntimeException re) {  
  10.     log.error("find by example failed", re);  
  11.     throw re;  
  12.    }  

findByProperty()方法用来灵活的提供一种按条件查询的方法,你可以自己定义要按什么样的方 式查询.

  1. public List findByProperty(String propertyName, Object value) {  
  2.    log.debug("finding User instance with property: " + propertyName  
  3.      + ", value: " + value);  
  4.    try {  
  5.     String queryString = "from User as model where model." 
  6.       + propertyName + "= ?";  
  7.     Query queryObject = getSession().createQuery(queryString);  
  8.     queryObject.setParameter(0, value);  
  9.     return queryObject.list();  
  10.    } catch (RuntimeException re) {  
  11.     log.error("find by property name failed", re);  
  12.     throw re;  
  13.    }  
  14. }  
  15.  
  16.  
  17. public List findByName(Object name) {  
  18.    return findByProperty(NAME, name);  
  19. }  
  20.  
  21. public List findBySex(Object sex) {  
  22.    return findByProperty(SEX, sex);  
  23. }  
  24.  
  25. public List findByAge(Object age) {  
  26.    return findByProperty(AGE, age);  
  27. }  
  28.  
  29. public List findAll() {  
  30.    log.debug("finding all User instances");  
  31.    try {  
  32.     String queryString = "from User";  
  33.     Query queryObject = getSession().createQuery(queryString);  
  34.     return queryObject.list();  
  35.    } catch (RuntimeException re) {  
  36.     log.error("find all failed", re);  
  37.     throw re;  
  38.    }  
  39. }  

将传入的detached状态的对象的属性复制到持久化对象中,并返回该持久化对象   如果该session中没有关联的持久化对象,加载一个,如果传入对象未保存,保存一个副本并作为持久对象返回,传入对象依然保持detached状态。

可以用作更新数据

  1. public User merge(User detachedInstance) {  
  2.    log.debug("merging User instance");  
  3.    try {  
  4.  
  5.     Session session=getSession();  
  6.     Transaction tx=session.beginTransaction();  
  7.      
  8.     User result = (User) session.merge(detachedInstance);  
  9.     tx.commit();  
  10.     session.close();  
  11.     log.debug("merge successful");  
  12.     return result;  
  13.    } catch (RuntimeException re) {  
  14.     log.error("merge failed", re);  
  15.     throw re;  
  16.    }  
  17. }  

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

  1. public void attachDirty(User instance) {  
  2.    log.debug("attaching dirty User instance");  
  3.    try {  
  4.     getSession().saveOrUpdate(instance);  
  5.     log.debug("attach successful");  
  6.    } catch (RuntimeException re) {  
  7.     log.error("attach failed", re);  
  8.     throw re;  
  9.    }  

将传入的对象状态设置为Transient状态

  1. public void attachClean(User instance) {  
  2.    log.debug("attaching clean User instance");  
  3.    try {  
  4.     getSession().lock(instance, LockMode.NONE);  
  5.     log.debug("attach successful");  
  6.    } catch (RuntimeException re) {  
  7.     log.error("attach failed", re);  
  8.     throw re;  
  9.    }  

【编辑推荐】

  1. Hibernate对各数据库的连接方言
  2. Hibernate支持Access方言源代码
  3. Hibernate不同数据库的连接及SQL方言
  4. hibernate中update与saveOrUpdate的区别
  5. HIBERNATE方言

 

责任编辑:book05 来源: 百度博客
相关推荐

2019-03-27 11:30:30

Linux终端密码生成器

2009-06-29 17:03:41

自动生成Getter和Eclipse

2022-09-20 10:50:34

PandasNumPy

2011-07-13 09:42:45

密码crypt

2009-07-14 17:12:26

ibatis自动代码生

2017-01-05 14:01:38

linux密码高强度

2021-01-26 12:36:34

Pycharm系统技巧项目缓存

2023-07-27 13:31:14

Linux语言数据

2013-06-27 14:57:58

Eclipse超酷插件移动开发

2009-06-08 20:07:44

Eclipse中使用p

2020-09-16 10:16:54

数据分析量化大数据

2014-06-19 10:59:10

AndroidEclipse公共库

2009-06-17 17:44:41

Eclipse插件Sp

2020-07-03 18:14:20

JavaScript开发技术

2009-09-29 10:01:59

Eclipse插件安装

2021-11-04 11:54:30

Linux内存系统

2009-02-26 09:55:39

2024-04-01 13:08:24

唯一IDC#后端

2009-09-22 17:38:25

Jobs框架

2011-03-14 17:36:12

DB2更新执行计划
点赞
收藏

51CTO技术栈公众号