{"id":1440,"date":"2010-11-29T03:31:18","date_gmt":"2010-11-29T03:31:18","guid":{"rendered":"http:\/\/enjoyasp.net\/?p=1440"},"modified":"2010-11-29T03:31:18","modified_gmt":"2010-11-29T03:31:18","slug":"%e6%9f%a5%e8%af%a2","status":"publish","type":"post","link":"https:\/\/enjoyasp.net\/index.php\/2010\/11\/29\/%e6%9f%a5%e8%af%a2\/","title":{"rendered":"\u67e5\u8be2"},"content":{"rendered":"<p>1\uff09CRUD\u64cd\u4f5c\uff1a<br \/>\nusing (ISession session = NHibernateHelper.OpenSession())<br \/>\nusing (ITransaction transaction = session.BeginTransaction())<br \/>\n{<br \/>\nsession.Update(product); \/\/session.Save(product);session.Delete(product); \u6216\u8005_session.SaveOrUpdate(c);<br \/>\ntransaction.Commit();<br \/>\n}<\/p>\n<p>\u5728NHibernate\u4e2d\u63d0\u4f9b\u4e86\u4e09\u79cd\u67e5\u8be2\u65b9\u5f0f\u7ed9\u6211\u4eec\u9009\u62e9\uff1aNHibernate\u67e5\u8be2\u8bed\u8a00(HQL\uff0cNHibernate Query  Language)\u3001\u6761\u4ef6\u67e5\u8be2(Criteria API\uff0cCriteria Query)\u3001(\u6839\u636e\u793a\u4f8b\u67e5\u8be2(QBE\uff0cQuery By Example)\u662f\u6761\u4ef6\u67e5\u8be2\u7684\u4e00\u79cd\u7279\u6b8a\u60c5\u51b5)\u3001\u539f\u751fSQL(Literal SQL\uff0cT-SQL\u3001PL\/SQL)\u3002<\/p>\n<p>2)\u7b80\u5355\u67e5\u8be2&#8211;\u6761\u4ef6\u67e5\u8be2Criteria Query<\/p>\n<ul>\n<li> \u636e\u5173\u952e\u5b57\u67e5\u8be2\uff1asession.Get&lt;Product&gt;(productId);<\/li>\n<li>\u636e\u5176\u5b83\u5c5e\u6027\u67e5\u8be2<\/li>\n<li> \u8fd4\u56de\u4e00\u4e2a\u503c\uff1asession.CreateCriteria(typeof(Product)).Add(Restrictions.Eq(&#8220;Name&#8221;, name)).UniqueResult&lt;Product&gt;();<\/li>\n<li> \u8fd4\u56de\u4e00\u4e2a\u96c6\u5408\uff1asession.CreateCriteria(typeof(Product)).Add(Restrictions.Eq(&#8220;Name&#8221;, name)).List&lt;Product&gt;();<\/li>\n<li>\u9664\u4e86\u7528Eq\u5916\uff0c\u8fd8\u53ef\u7528like,between\u7b49<\/li>\n<\/ul>\n<p>3\uff09SQL\u8bed\u53e5&#8211;HQL\u67e5\u8be2<\/p>\n<ul>\n<li>From\u5b50\u53e5\uff1a  return _session.CreateQuery(&#8220;from Customer&#8221;)        .List&lt;Customer&gt;();<\/li>\n<li>\n<pre>select \u5b50\u53e5 \uff1a\r\n<\/pre>\n<\/li>\n<li>\n<pre>return _session.CreateQuery(\"select c.Firstname, count(c.Firstname) from Customer c group by c.Firstname\").List&lt;object[]&gt;();\r\n<\/pre>\n<\/li>\n<li>where\u5b50\u53e5\uff1areturn _session.CreateQuery(&#8220;from Customer c where c.Firstname=&#8217;YJing'&#8221;)        .List&lt;Customer&gt;();<br \/>\n\u6ce8\uff1a\u663e\u793a\u7684\u540d\u5b57\u4e3a\u5b9e\u4f53\u540d\u800c\u4e0d\u662f\u8868\u540d<\/li>\n<li>\n<pre>  order by\u5b50\u53e5\uff1areturn _session.CreateQuery(\"from Customer c order by c.Firstname asc,c.Lastname desc\").List&lt;Customer&gt;();<\/pre>\n<\/li>\n<li>group by\u5b50\u53e5\uff1a\n<pre>  return _session.CreateQuery(\"select c.Firstname, count(c.Firstname) from Customer c group by c.Firstname\").List&lt;object[]&gt;();<\/pre>\n<\/li>\n<li>\u53c2\u6570\u4f20\u9012\uff1a\n<pre> return _session.CreateQuery(\"from Customer c where c.Firstname=:fn\").SetString(\"fn\", firstname).List&lt;Customer&gt;();<\/pre>\n<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>1\uff09CRUD\u64cd\u4f5c\uff1a using (ISession session = NHibernateHelper.Op [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[42],"tags":[],"class_list":["post-1440","post","type-post","status-publish","format-standard","hentry","category-nhibernate"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/posts\/1440","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/comments?post=1440"}],"version-history":[{"count":0,"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/posts\/1440\/revisions"}],"wp:attachment":[{"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/media?parent=1440"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/categories?post=1440"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/tags?post=1440"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}