Eclipse下生成HibernateDAO中的几个方法.doc_第1页
Eclipse下生成HibernateDAO中的几个方法.doc_第2页
Eclipse下生成HibernateDAO中的几个方法.doc_第3页
Eclipse下生成HibernateDAO中的几个方法.doc_第4页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

Eclipse下生成HibernateDAO中的几个方法 2009-06-24 08:03 佚名 百度博客 我要评论(0) 本文介绍了Eclipse下生成HibernateDAO中的几个方法,如save()方法,delete()方法,findByExample()方法等。* save()方法提供了向数据库中添加数据的功能,但只能添加,这个DAO没有生成Update()的方法* 但你可以简单的把save()方法改称具有Update功能:将getSession().save * (transientInstance);这句改成* getSession().merge(transientInstance);或者getSession().saveOrUpdate* (transientInstance); 1. publicvoidsave(UsertransientInstance) 2. log.debug(savingUserinstance); 3. try 4. Sessionsession=getSession(); 5. Transactiontx=session.beginTransaction(); 6. session.save(transientInstance); 7. mit(); 8. session.close(); 9. log.debug(savesuccessful); 10. catch(RuntimeExceptionre) 11. log.error(savefailed,re); 12. throwre; 13. 14. delete()方法用来删除的 实际上我们会用下边的这个方法进行删除15. publicvoiddelete(Integerid) 16. log.debug(deletingUserinstance.); 17. Useruser=findById(id); 18. delete(user); 19. 20. 21. publicvoiddelete(UserpersistentInstance) 22. log.debug(deletingUserinstance); 23. try 24. Sessionsession=getSession(); 25. Transactiontx=session.beginTransaction(); 26. session.delete(persistentInstance); 27. mit(); 28. session.close(); 29. log.debug(deletesuccessful); 30. catch(RuntimeExceptionre) 31. log.error(deletefailed,re); 32. throwre; 33. 34. 根据编号进行查找35. publicUserfindById(java.lang.Integerid) 36. log.debug(gettingUserinstancewithid:+id); 37. try 38. Userinstance=(User)getSession().get(hbm.User,id); 39. returninstance; 40. catch(RuntimeExceptionre) 41. log.error(getfailed,re); 42. throwre; 43. 44. findByExample()方法实现的功能相当于select * from Usertable实现的功能就是查询所有 数据.45. publicListfindByExample(Userinstance) 46. log.debug(findingUserinstancebyexample); 47. try 48. Listresults=getSession().createCriteria(hbm.User).add( 49. Example.create(instance).list(); 50. log.debug(findbyexamplesuccessful,resultsize:51. +results.size(); 52. returnresults; 53. catch(RuntimeExceptionre) 54. log.error(findbyexamplefailed,re); 55. throwre; 56. 57. findByProperty()方法用来灵活的提供一种按条件查询的方法,你可以自己定义要按什么样的方 式查询.58. publicListfindByProperty(StringpropertyName,Objectvalue) 59. log.debug(findingUserinstancewithproperty:+propertyName 60. +,value:+value); 61. try 62. StringqueryString=fromUserasmodelwheremodel.63. +propertyName+=?; 64. QueryqueryObject=getSession().createQuery(queryString); 65. queryObject.setParameter(0,value); 66. returnqueryObject.list(); 67. catch(RuntimeExceptionre) 68. log.error(findbypropertynamefailed,re); 69. throwre; 70. 71. 72. 73. 74. publicListfindByName(Objectname) 75. returnfindByProperty(NAME,name); 76. 77. 78. publicListfindBySex(Objectsex) 79. returnfindByProperty(SEX,sex); 80. 81. 82. publicListfindByAge(Objectage) 83. returnfindByProperty(AGE,age); 84. 85. 86. publicListfindAll() 87. log.debug(findingallUserinstances); 88. try 89. StringqueryString=fromUser; 90. QueryqueryObject=getSession().createQuery(queryString); 91. returnqueryObject.list(); 92. catch(RuntimeExceptionre) 93. log.error(findallfailed,re); 94. throwre; 95. 96. 将传入的detached状态的对象的属性复制到持久化对象中,并返回该持久化对象 如果该session中没有关联的持久化对象,加载一个,如果传入对象未保存,保存一个副本并作为持久对象返回,传入对象依然保持detached状态。 可以用作更新数据97. publicUsermerge(UserdetachedInstance) 98. log.debug(mergingUserinstance); 99. try 100. 101. Sessionsession=getSession(); 102. Transactiontx=session.beginTransaction(); 103. 104. Userresult=(User)session.merge(detachedInstance); 105. mit(); 106. session.close(); 107. log.debug(mergesuccessful); 108. returnresult; 109. catch(RuntimeExceptionre) 110. log.error(mergefailed,re); 111. throwre; 112. 113. 将传入的对象持久化并保存。 如果对象未保存(Transient状态),调用save方法保存。如果对象已保存(Detached状态),调用update方法将对象与Session重新关联。114. publicvoidattachDirty(Userinstance) 115. log.debug(attachingdirtyUserinstance); 116. try 117. getSession().saveOrUpdate(instance); 118. log.debug(attachsuccessful); 119. catch(RuntimeExceptionre) 120. log.error(attachfailed,re); 121. throwre; 122. 123. 将传入的对象状态设置为Transient状态 124. publicvoidattachClean(Userinstance) 125. log.debug(attachin

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论