自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Eclipse下生成HibernateDAO中的幾個方法

開發(fā) 后端
本文介紹了Eclipse下生成HibernateDAO中的幾個方法,如save()方法,delete()方法,findByExample()方法等。

* save()方法提供了向數(shù)據(jù)庫中添加數(shù)據(jù)的功能,但只能添加,這個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. }  

根據(jù)編號進行查找

  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()方法實現(xiàn)的功能相當于"select * from Usertable"實現(xiàn)的功能就是查詢所有 數(shù)據(jù).

  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狀態(tài)的對象的屬性復制到持久化對象中,并返回該持久化對象   如果該session中沒有關(guān)聯(lián)的持久化對象,加載一個,如果傳入對象未保存,保存一個副本并作為持久對象返回,傳入對象依然保持detached狀態(tài)。

可以用作更新數(shù)據(jù)

  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狀態(tài)),調(diào)用save方法保存。如果對象已保存(Detached狀態(tài)),調(diào)用update方法將對象與Session重新關(guān)聯(lián)。

  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.    }  

將傳入的對象狀態(tài)設置為Transient狀態(tài)

  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對各數(shù)據(jù)庫的連接方言
  2. Hibernate支持Access方言源代碼
  3. Hibernate不同數(shù)據(jù)庫的連接及SQL方言
  4. hibernate中update與saveOrUpdate的區(qū)別
  5. HIBERNATE方言

 

責任編輯:book05 來源: 百度博客
相關(guān)推薦

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自動代碼生

2013-06-27 14:57:58

Eclipse超酷插件移動開發(fā)

2017-01-05 14:01:38

linux密碼高強度

2024-08-06 11:40:57

2023-07-27 13:31:14

Linux語言數(shù)據(jù)

2021-01-26 12:36:34

Pycharm系統(tǒng)技巧項目緩存

2009-06-08 20:07:44

Eclipse中使用p

2020-09-16 10:16:54

數(shù)據(jù)分析量化大數(shù)據(jù)

2014-06-19 10:59:10

AndroidEclipse公共庫

2009-06-17 17:44:41

Eclipse插件Sp

2021-11-04 11:54:30

Linux內(nèi)存系統(tǒng)

2009-09-29 10:01:59

Eclipse插件安裝

2009-02-26 09:55:39

2009-09-22 17:38:25

Jobs框架

2022-04-06 07:32:41

Java運算符變量

2011-03-14 17:36:12

DB2更新執(zhí)行計劃
點贊
收藏

51CTO技術(shù)棧公眾號