hibernate 自動產生 dao 包含的幾個方法

來源:互聯網
上載者:User

 

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.