您好,登錄后才能下訂單哦!
這篇文章給大家介紹Eclipse中怎么生成HibernateDAO,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。
* save()方法提供了向數(shù)據(jù)庫(kù)中添加數(shù)據(jù)的功能,但只能添加,這個(gè)DAO沒(méi)有生成Update()的方法
* 但你可以簡(jiǎn)單的把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()方法用來(lái)刪除的 實(shí)際上我們會(huì)用下邊的這個(gè)方法進(jìn)行刪除
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; } }
根據(jù)編號(hào)進(jìn)行查找
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()方法實(shí)現(xiàn)的功能相當(dāng)于"select * from Usertable"實(shí)現(xiàn)的功能就是查詢所有 數(shù)據(jù).
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()方法用來(lái)靈活的提供一種按條件查詢的方法,你可以自己定義要按什么樣的方 式查詢.
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狀態(tài)的對(duì)象的屬性復(fù)制到持久化對(duì)象中,并返回該持久化對(duì)象 如果該session中沒(méi)有關(guān)聯(lián)的持久化對(duì)象,加載一個(gè),如果傳入對(duì)象未保存,保存一個(gè)副本并作為持久對(duì)象返回,傳入對(duì)象依然保持detached狀態(tài)。
可以用作更新數(shù)據(jù)
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; } }
將傳入的對(duì)象持久化并保存。 如果對(duì)象未保存(Transient狀態(tài)),調(diào)用save方法保存。如果對(duì)象已保存(Detached狀態(tài)),調(diào)用update方法將對(duì)象與Session重新關(guān)聯(lián)。
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; } }
將傳入的對(duì)象狀態(tài)設(shè)置為Transient狀態(tài)
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; } }
關(guān)于Eclipse中怎么生成HibernateDAO就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。