欢迎来到天天文库
浏览记录
ID:8808451
大小:49.50 KB
页数:4页
时间:2018-04-08
《eclipse下生成hibernatedao中的几个方法》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、·Eclipse下生成HibernateDAO中的几个方法·http://developer.51cto.com 2009-06-2408:03 佚名 百度博客 我要评论(0)本文介绍了Eclipse下生成HibernateDAO中的几个方法,如save()方法,delete()方法,findByExample()方法等。*save()方法提供了向数据库中添加数据的功能,但只能添加,这个DAO没有生成Update()的方法*但你可以简单的把save()方法改称具有Update功能:将getSession().save*(transi
2、entInstance);这句改成*getSession().merge(transientInstance);或者getSession().saveOrUpdate* (transientInstance);1.public void save(User transientInstance) { 2. log.debug("saving User instance"); 3. try { 4. Session session=getSession(); 5. Transaction tx=session.be
3、ginTransaction(); 6. session.save(transientInstance); 7. tx.commit(); 8. session.close(); 9. log.debug("save successful"); 10. } catch (RuntimeException re) { 11. log.error("save failed", re); 12. throw re; 13. } 14.} delete()方法用来删除的实际上我们会用下边的这个方法
4、进行删除15.public void delete(Integer id){ 16. log.debug("deleting User instance..."); 17. User user=findById(id); 18. delete(user); 19.} 20. 21.public void delete(User persistentInstance) { 22. log.debug("deleting User instance"); 23. try { 24. Session sessio
5、n=getSession(); 25. Transaction tx=session.beginTransaction(); 26. session.delete(persistentInstance); 27. tx.commit(); 28. session.close(); 1. log.debug("delete successful"); 2. } catch (RuntimeException re) { 3. log.error("delete failed", re); 4.
6、 throw re; 5. } 6.} 根据编号进行查找7.public User findById(java.lang.Integer id) { 8. log.debug("getting User instance with id: " + id); 9. try { 10. User instance = (User) getSession().get("hbm.User", id); 11. return instance; 12. } catch (RuntimeException re)
7、 { 13. log.error("get failed", re); 14. throw re; 15. } 16.} findByExample()方法实现的功能相当于"select*fromUsertable"实现的功能就是查询所有数据.17.public List findByExample(User instance) { 18. log.debug("finding User instance by example"); 19. try { 20. List results = getSes
8、sion().createCriteria("hbm.User").add( 21. Example.create(instance)).list(); 22. log.debug("find by example s
此文档下载收益归作者所有