锘??xml version="1.0" encoding="utf-8" standalone="yes"?> * save()鏂規硶鎻愪緵浜嗗悜鏁版嵁搴撲腑娣誨姞鏁版嵁鐨勫姛鑳?浣嗗彧鑳芥坊鍔?榪欎釜DAO娌℃湁鐢熸垚Update()鐨勬柟娉?br />
* 浣嗕綘鍙互綆鍗曠殑鍏玸ave()鏂規硶鏀圭О鍏鋒湁Update鍔熻兘:灝唃etSession().save * (transientInstance);榪欏彞鏀規垚 public void save(User transientInstance) {
* getSession().merge(transientInstance);鎴栬単etSession().saveOrUpdate
* (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"瀹炵幇鐨勫姛鑳藉氨鏄煡璇㈡墍鏈?nbsp;鏁版嵁.
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鐘舵佺殑瀵硅薄鐨勫睘鎬у鍒跺埌鎸佷箙鍖栧璞′腑錛屽茍榪斿洖璇ユ寔涔呭寲瀵硅薄 濡傛灉璇ession涓病鏈夊叧鑱旂殑鎸佷箙鍖栧璞★紝鍔犺澆涓涓紝濡傛灉浼犲叆瀵硅薄鏈繚瀛橈紝淇濆瓨涓涓壇鏈茍浣滀負鎸佷箙瀵硅薄榪斿洖錛屼紶鍏ュ璞′緷鐒朵繚鎸乨etached鐘舵併?nbsp;
鍙互鐢ㄤ綔鏇存柊鏁版嵁
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;
}
}
灝嗕紶鍏ョ殑瀵硅薄鎸佷箙鍖栧茍淇濆瓨銆?nbsp;濡傛灉瀵硅薄鏈繚瀛橈紙Transient鐘舵侊級錛岃皟鐢╯ave鏂規硶淇濆瓨銆傚鏋滃璞″凡淇濆瓨錛圖etached鐘舵侊級錛岃皟鐢╱pdate鏂規硶灝嗗璞′笌Session閲嶆柊鍏寵仈銆?/font>
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鐘舵?nbsp;
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;
}
}