這篇文章給大家介紹Eclipse中怎么生成HibernateDAO,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

網站建設哪家好,找創新互聯!專注于網頁設計、網站建設、微信開發、成都微信小程序、集團企業網站建設等服務項目。為回饋新老客戶創新互聯還提供了南皮免費建站歡迎大家使用!
* save()方法提供了向數據庫中添加數據的功能,但只能添加,這個DAO沒有生成Update()的方法
* 但你可以簡單的把save()方法改稱具有Update功能:將getSession().save * (transientInstance);這句改成
* getSession().merge(transientInstance);或者getSession().saveOrUpdate
*   (transientInstance);
public void save(User transientInstance) {     log.debug("saving User instance");     try {      Session session=getSession();      Transaction tx=session.beginTransaction();      session.save(transientInstance);      tx.commit();      session.close();      log.debug("save successful");     } catch (RuntimeException re) {      log.error("save failed", re);      throw re;     }  }delete()方法用來刪除的 實際上我們會用下邊的這個方法進行刪除
public void delete(Integer id){     log.debug("deleting User instance...");     User user=findById(id);     delete(user);  }   public void delete(User persistentInstance) {     log.debug("deleting User instance");     try {      Session session=getSession();      Transaction tx=session.beginTransaction();      session.delete(persistentInstance);      tx.commit();      session.close();      log.debug("delete successful");     } catch (RuntimeException re) {      log.error("delete failed", re);      throw re;     }  }根據編號進行查找
public User findById(java.lang.Integer id) {     log.debug("getting User instance with id: " + id);     try {      User instance = (User) getSession().get("hbm.User", id);      return instance;     } catch (RuntimeException re) {      log.error("get failed", re);      throw re;     }  }findByExample()方法實現的功能相當于"select * from Usertable"實現的功能就是查詢所有 數據.
public List findByExample(User instance) {     log.debug("finding User instance by example");     try {      List results = getSession().createCriteria("hbm.User").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 User instance with property: " + propertyName       + ", value: " + value);     try {      String queryString = "from User 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;     }  }    public List findByName(Object name) {     return findByProperty(NAME, name);  }   public List findBySex(Object sex) {     return findByProperty(SEX, sex);  }   public List findByAge(Object age) {     return findByProperty(AGE, age);  }   public List findAll() {     log.debug("finding all User instances");     try {      String queryString = "from User";      Query queryObject = getSession().createQuery(queryString);      return queryObject.list();     } catch (RuntimeException re) {      log.error("find all failed", re);      throw re;     }  }將傳入的detached狀態的對象的屬性復制到持久化對象中,并返回該持久化對象 如果該session中沒有關聯的持久化對象,加載一個,如果傳入對象未保存,保存一個副本并作為持久對象返回,傳入對象依然保持detached狀態。
可以用作更新數據
public User merge(User detachedInstance) {     log.debug("merging User instance");     try {       Session session=getSession();      Transaction tx=session.beginTransaction();           User result = (User) session.merge(detachedInstance);      tx.commit();      session.close();      log.debug("merge successful");      return result;     } catch (RuntimeException re) {      log.error("merge failed", re);      throw re;     }  }將傳入的對象持久化并保存。 如果對象未保存(Transient狀態),調用save方法保存。如果對象已保存(Detached狀態),調用update方法將對象與Session重新關聯。
public void attachDirty(User instance) {     log.debug("attaching dirty User instance");     try {      getSession().saveOrUpdate(instance);      log.debug("attach successful");     } catch (RuntimeException re) {      log.error("attach failed", re);      throw re;     }  }將傳入的對象狀態設置為Transient狀態
public void attachClean(User instance) {     log.debug("attaching clean User instance");     try {      getSession().lock(instance, LockMode.NONE);      log.debug("attach successful");     } catch (RuntimeException re) {      log.error("attach failed", re);      throw re;     }  }關于Eclipse中怎么生成HibernateDAO就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
                當前題目:Eclipse中怎么生成HibernateDAO
                
                URL分享:http://www.yijiale78.com/article24/ghdgce.html
            
成都網站建設公司_創新互聯,為您提供域名注冊、響應式網站、標簽優化、網站建設、云服務器、關鍵詞優化
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯