﻿{"id":191,"date":"2010-05-25T12:14:09","date_gmt":"2010-05-25T04:14:09","guid":{"rendered":"http:\/\/nick.workao.org\/?p=191"},"modified":"2021-11-17T11:24:54","modified_gmt":"2021-11-17T03:24:54","slug":"nhibernate%e6%9f%a5%e8%af%a2%e3%80%81%e6%9b%b4%e6%96%b0%e3%80%81%e5%88%a0%e9%99%a4","status":"publish","type":"post","link":"https:\/\/nick.txtcc.com\/index.php\/win\/191","title":{"rendered":"NHibernate\u67e5\u8be2\u3001\u66f4\u65b0\u3001\u5220\u9664"},"content":{"rendered":"<p>[TestFixture]<\/p>\n<p>public class  UserFixture {\nConfiguration cfg;\nISessionFactory factory ;\nreadonly string  userId=\u201dtest_user\u201d;<\/p>\n<p>\/\/\u51c6\u5907\u6d4b\u8bd5\u524d\u7684\u5de5\u4f5c\n[<strong>SetUp<\/strong>]\npublic void  SetUp() {<\/p>\n<p>\/\/Configuration\u5bf9\u8c61\u80fd\u591f\u89e3\u6790\u6240\u6709.Net\u5bf9\u8c61\u548c\u540e\u53f0\u6570\u636e\u5e93\u4e2d\u7684\u6620\u5c04 \u5173\u7cfb\u3002\ncfg = new  Configuration();<\/p>\n<p>\/\/Configuration\u5bf9\u8c61\u4f1a\u641c\u7d22\u88c5\u914d\u4ef6\u91cc\u7684\u4efb\u4f55\u4ee5hbm.xml \u7ed3\u5c3e\u7684\u6587\u4ef6\uff0c\u524d\u63d0\u662f\n\/\/*.hbm.xml\u8981\u7f16\u8bd1\u4e3a\u201c\u5d4c\u5957\u7684\u8d44\u6e90\u201d\ncfg.AddAssembly(\u201dNHibernate.Examples\u201d);\nfactory =  cfg.BuildSessionFactory();\n}<\/p>\n<p>[TearDown]<\/p>\n<p>public void TearDown() {\n\/\/\u6e05\u9664\u4e00\u4e9b\u64cd\u4f5c\u4e2d\u751f\u6210\u7684\u4e34\u65f6\u6570\u636e\n}<\/p>\n<p>\/\/\/<summary>\n\/\/\/<strong>\u6e05\u9664\u6570\u636e<\/strong>\u5e93\u4e2d Users\u8868\u4e2d\u7684\u6240\u6709\u6570\u636e\uff0c\u63d0\u4f9b\u4e00\u4e2a\u5e72\u51c0\u7684\u6d4b\u8bd5\u6570\u636e\u73af\u5883\n\/\/\/<\/summary>\n[Test]\npublic void  ClearData() {\n\/\/ISession\u5bf9\u8c61\u63d0\u4f9b\u4e00\u4e2a\u5230\u540e\u53f0\u6570\u636e\u5e93\u7684\u8fde\u63a5\uff0c\u53c2\u6570\u7531App.config\u6307\u5b9a\n\/\/ITransaction\u5bf9\u8c61\u63d0\u4f9b\u4e00\u4e2a\u53ef\u4ee5\u88abNHibernate\u7ba1\u7406\u7684\u4e8b \u52a1\u3002\nISession session = factory.OpenSession();\nITransaction  transaction = session.BeginTransaction();<\/p>\n<p>\/\/\u4f7f\u7528\u67e5\u8be2\u5220\u9664\u6240\u6709\u6570\u636e\nsession.Delete(\u201dSelect From User Where 1=1\u2033);<\/p>\n<p>transaction.Commit();<\/p>\n<p>session.Close();<\/p>\n<p>}<\/p>\n<p>\/\/\/<summary>\n\/\/\/<strong>\u6d4b\u8bd5\u5f80\u6570\u636e\u5e93\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u5bf9\u8c61<\/strong>\n\/\/\/<\/summary>\n[Test]\npublic void  AddTest() {\nISession session =  factory.OpenSession();\nITransaction  transaction = session.BeginTransaction();<\/p>\n<p>User newUser  = new User();\nnewUser.Id = userId;\nnewUser.UserName =  \u201cSkyDev\u201d;\nnewUser.Password =  \u201cabc123\u2033;\nnewUser.EmailAddress =  <a href=\"mailto:skydev@pyp.edu.cn\">skydev@pyp.edu.cn<\/a>;\nnewUser.LastLogon =  DateTime.Now;\nsession.Save(newUser);<\/p>\n<p>transaction.Commit();\nsession.Close();\n}<\/p>\n<p>\/\/\/<summary>\n\/\/\/<strong>\u66f4\u65b0\u6570\u636e\u5e93\u4e2d\u7684\u5bf9\u8c61\n<\/strong> \/\/\/<\/summary>\n[Test]\npublic void  UpdateTest() {\nISession session =  factory.OpenSession();\nITransaction  transaction = session.BeginTransaction();<\/p>\n<p>User aUser;<\/p>\n<p>try {<\/p>\n<p>\/\/\u5f53userId\u6307\u5b9a\u7684\u6570\u636e\u5728\u6570\u636e\u5e93\u4e2d\u4e0d\u5b58\u5728\u65f6\u4f1a\u629b\u51fa\u5f02\u5e38\naUser= (User)session.Load(typeof(User), userId);<\/p>\n<p>\/\/ set Joe Cool\u2019s Last Login property\naUser.LastLogon = DateTime.Now;<\/p>\n<p>\/\/ flush the changes from the Session to  the Database\nsession.Flush();<\/p>\n<p>transaction.Commit();\n}\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 catch {\u00a0 transaction.Rollback();\u00a0}<\/p>\n<p>finally {\u00a0\u00a0session.Close();\u00a0}\n}<\/p>\n<p>\/\/\/<summary>\n\/\/\/<strong>\u5220\u9664\u6307\u5b9a\u7684\u5bf9\u8c61<\/strong>\n\/\/\/<\/summary>\n[Test]\npublic void  DeleteTest() {\nISession session =  factory.OpenSession();\nITransaction  transaction = session.BeginTransaction();<\/p>\n<p>User aUser;<\/p>\n<p>try {\n\/\/\u5f53userId\u6307\u5b9a\u7684\u6570\u636e\u5728\u6570\u636e\u5e93\u4e2d\u4e0d\u5b58\u5728\u65f6\u4f1a\u629b\u51fa\u5f02\u5e38\naUser = session.Load(typeof(User), userId) as User;<\/p>\n<p>if (aUser!=null) { session.Delete(aUser); }<\/p>\n<p>transaction.Commit();\n}<\/p>\n<p>catch { transaction.Rollback();\u00a0 }<\/p>\n<p>finally { session.Close();\u00a0 }<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>\u6b63\u5982\u4f60\u6240\u770b\u5230\u7684\uff0c\u5173\u4e8eNHiberante\u91cd\u8981\u7684\u4e8b\u60c5\u662f\u5982\u6b64\u7b80\u5355\u3002\u7ee7\u7eed\u5e76\u4e14\u67e5\u8be2\u4f60\u7684\u6570\u636e\u5e93\uff0c\u9a8c\u8bc1\u4e00\u4e0bUser\u8868\u91cc\u7684\u65b0\u8bb0\u5f55\u3002 \u73b0\u5728\u91cd\u8981\u7684\u4e8b\u60c5\u5c31\u662f\u4f60\u53bb\u64cd\u5fc3\u4e1a\u52a1\u5bf9\u8c61\u5e76\u5728\u8fdb\u884c\u5904\u7406\u7684\u65f6\u5019\u544a\u8bc9NHibernate\u5c31\u53ef\u4ee5\u4e86\u3002<\/p>\n<p>\u8ba9\u6211\u4eec\u6765\u544a\u8bc9\u4f60\uff0c<strong>\u5f53\u4f60\u6709\u4e00\u4e2a<\/strong>\n<strong>UserID\u7684\u65f6\u5019\u5982\u4f55\u83b7\u53d6\u5bf9\u8c61<\/strong>\uff08\u4e3e \u4f8b\u8bf4\uff0c\u767b\u9646\u4f60\u7684\u7f51\u7ad9\u7684\u65f6\u5019\uff09\u3002\u4ec5\u4ec5\u4e00\u53e5\u8bdd\u5c31\u53ef\u4ee5\u6253\u5f00Session\uff0c\u4f20\u5165key\u5c31\u53ef\u4ee5\u4e86<\/p>\n<p>\/\/ open another session to retrieve the just  inserted user\nsession =  factory.OpenSession();\nUser joeCool =  (User)session.Load(typeof(User), \u201cjoe_cool\u201d);<\/p>\n<p>\u4f60\u6240\u83b7\u53d6\u7684User\u5bf9\u8c61\u8fd8\u5728\u751f\u5b58\u5468\u671f\u5185\uff01\u6539\u53d8\u5b83\u7684\u5c5e\u6027\uff0c\u5e76\u901a\u8fc7Flush()\u6301\u4e45\u5316\u5230\u6570\u636e\u5e93\u3002<\/p>\n<p>\/\/ set Joe Cool\u2019s Last Login property\njoeCool.LastLogon = DateTime.Now;<\/p>\n<p>\/\/ flush the changes from the Session to the  Database\nsession.Flush();<\/p>\n<p>\u4f60\u6240\u8981\u505a\u7684\u5c31\u662f\u901a\u8fc7NHibernate\u6765\u8fdb\u884c\u4f60\u9700\u8981\u7684\u6539\u53d8\uff0c<strong>\u5e76\u8c03\u7528Session\u7684Flush()\u65b9\u6cd5\u63d0 \u4ea4<\/strong>\u3002\u9a8c\u8bc1\u4e00\u4e0b\u6570\u636e\u5e93\uff0c\u67e5\u67e5\u7528\u6237ID\u4e3a\u201djoe_cool\u201d\u7684\u8bb0\u5f55\u4e2d\u201dLastLogon\u201d\u7684\u66f4\u6539\u3002<\/p>\n<p>\u8fd8\u6709\u66f4\u597d\u7684\uff0c\u4f60\u53ef\u4ee5\u4ee5System.Collections.IList\u7684\u65b9\u5f0f\u6765\u83b7\u53d6\u4ece\u8868\u4e2d\u7684\u5bf9\u8c61\u3002\u5982\u4e0b\nIList userList =  session.CreateCriteria(typeof(User)).List();\nforeach(User user in userList)\n{\nSystem.Diagnostics.Debug.WriteLine(user.Id + \u201d  last logged in at \u201d + user.LastLogon);\n}<\/p>\n<p><strong>\u8fd9\u4e2a\u67e5\u8be2\u5c06\u4f1a\u8fd4\u56de\u6240\u6709\u8868\u8bb0\u5f55\u3002\u5f80\u5f80\u4f60\u9700\u8981\u505a\u66f4\u591a\u7684\u63a7\u5236\uff0c\u6bd4\u5982\u8bf4\u83b7\u53d6\u4ece<\/strong>\n<strong>March  14, 2004 10:00 PM\u3000\u4ee5\u540e\u767b\u9646\u7684\u7528\u6237<\/strong>\uff0c\u5982\u4e0b\uff1a<\/p>\n<p>IList recentUsers = session.CreateCriteria(typeof(User))\n.Add(Expression.Expression.Gt(\u201dLastLogon\u201d, new DateTime(2004, 03, 14,  20, 0, 0)))\n.List();<\/p>\n<p>foreach(User user in recentUsers)\n{\nSystem.Diagnostics.Debug.WriteLine(user.Id + \u201d last logged in at \u201d +  user.LastLogon);\n}<\/p>\n<p>\u6587\u6863\u91cc\u8fd8\u6709\u4e00\u5806\u5065\u58ee\u7684\u67e5\u8be2\u65b9\u5f0f\u8ba9\u4f60\u8c03\u7528\uff0c\u8fd9\u91cc\u4ec5\u4ec5\u8ba9\u4f60\u5bf9NHibernate \u6240\u63d0\u4f9b\u7684\u5f3a\u6709\u529b\u7684\u5de5\u5177\u6709\u4e00\u5b9a\u7684\u4e86\u89e3\u3002<\/p>\n<p>\u6700\u540e\u8c03\u7528Session\u5bf9\u8c61\u7684Close()\u65b9\u6cd5\uff0c\u91ca\u653eNHibernate\u6240\u4f7f\u7528\u7684ADO.Net\u8fde\u63a5\u8d44\u6e90\n\/\/ tell NHibernate to close this  Session\nsession.Close();<\/p>","protected":false},"excerpt":{"rendered":"<p>[TestFixture] public class UserFixture { Configuration cfg; ISessionFactory factory ; readonly strin&#46;&#46;&#46;<\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[444],"class_list":["post-191","post","type-post","status-publish","format-standard","hentry","category-win","tag-nhibernate"],"_links":{"self":[{"href":"https:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/posts\/191","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/comments?post=191"}],"version-history":[{"count":2,"href":"https:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/posts\/191\/revisions"}],"predecessor-version":[{"id":2209,"href":"https:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/posts\/191\/revisions\/2209"}],"wp:attachment":[{"href":"https:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/media?parent=191"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/categories?post=191"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/tags?post=191"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}