1. 程式人生 > >Hibernate DAO中的幾個方法

Hibernate DAO中的幾個方法

* 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(transientInstanc); 
   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; 
  }