{"id":1379,"date":"2010-11-19T02:39:25","date_gmt":"2010-11-19T02:39:25","guid":{"rendered":"http:\/\/enjoyasp.net\/?p=1379"},"modified":"2010-11-23T01:32:02","modified_gmt":"2010-11-23T01:32:02","slug":"nhibernate%e5%85%a5%e9%97%a8","status":"publish","type":"post","link":"https:\/\/enjoyasp.net\/index.php\/2010\/11\/19\/nhibernate%e5%85%a5%e9%97%a8\/","title":{"rendered":"Nhibernate\u5165\u95e8"},"content":{"rendered":"<pre lang=\"csharp\">\r\n\u4e0b\u8f7d\uff1anhibernate\r\n\r\n\u4f7f\u7528\u65b9\u5f0f\uff1a\r\n1\uff0c\u9879\u76ee\u5f15\u5165dll:\r\n\r\n    * Antlr3.Runtime.dll\r\n    * Castle.Core.dll\r\n    * Iesi.Collections.dll\r\n    * LinFu.DynamicProxy.dll\r\n    * log4net.dll\r\n    * NHibernate.ByteCode.Castle.dll\r\n    * NHibernate.ByteCode.LinFu.dll\r\n    * NHibernate.dll\r\n    * nunit.core.dll\r\n    * nunit.framework.dll\r\n\r\n2\uff0c\u7f16\u5199\u6570\u636e\u5e93\u8fde\u63a5\uff1a\u5bf9\u4e8e\u4e00\u822c\u7684\u5e94\u7528\u7a0b\u5e8f\uff0c\u7f16\u5199hibernate.cfg.xml\u653e\u5728\u6839\u76ee\u5f55\u4e0b\uff0c\u5c5e\u6027\u8bbe\u7f6e\u4e3a\u59cb\u7ec8\u590d\u5236\uff0c\u56e0\u4e3anhibernate\u662f\u5bf9\u8f93\u51fa\u76ee\u5f55bin\u7684\u6587\u4ef6\u8fdb\u884c\u8bfb\u53d6\u7684\uff0c\u5bf9\u4e8eweb\u53ef\u5728web.config\u4e2d\u8fdb\u884c\u6dfb\u52a0\u3002\r\n      1\uff09hibernate.cfg.xml\uff0c\u5728\u5b98\u65b9\u6587\u6863\u7684Configuration_Templates\u6587\u4ef6\u5939\u4e0b\u6709\u6a21\u677f\uff0c\u4f8b\u5b50\u5982\u4e0b\uff1a\r\n       <?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<!--\r\nThis template was written to work with NHibernate.Test.\r\nCopy the template to your NHibernate.Test project folder and rename it in hibernate.cfg.xml and change it\r\nfor your own use before compile tests in VisualStudio.\r\n-->\r\n<!-- This is the System.Data.dll provider for SQL Server -->\r\n<hibernate-configuration  xmlns=\"urn:nhibernate-configuration-2.2\" >\r\n  <session-factory name=\"NHibernate.Test\">\r\n    <property name=\"connection.driver_class\">NHibernate.Driver.SqlClientDriver<\/property>\r\n    <property name=\"connection.connection_string\">\r\n      Server=(local);initial catalog=nhibernate;Integrated Security=SSPI;user id=sa;password=123abc;min pool size=1;max pool size=512\r\n    <\/property>\r\n    <property name=\"adonet.batch_size\">10<\/property>\r\n    <property name=\"show_sql\">false<\/property>\r\n    <property name=\"dialect\">NHibernate.Dialect.MsSql2000Dialect<\/property>\r\n    <property name=\"use_outer_join\">true<\/property>\r\n    <property name=\"command_timeout\">60<\/property>\r\n    <property name=\"query.substitutions\">true 1, false 0, yes 'Y', no 'N'<\/property>\r\n    <property name=\"proxyfactory.factory_class\">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu<\/property>\r\n  <\/session-factory>\r\n<\/hibernate-configuration>\r\n \r\n     2\uff09 web.config \u5728<configuration>\u4e0b\u8fdb\u884cNhibernate \u914d\u7f6e\r\n<configSections>\r\n            <section name=\"nhibernate\" type=\"System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089\" \/>\r\n           \r\n    <\/configSections>\r\n   \r\n    <nhibernate>\r\n        <add\r\n            key=\"hibernate.show_sql\"\r\n            value=\"true\"\r\n        \/>\r\n        <add\r\n            key=\"hibernate.connection.provider\"         \r\n            value=\"NHibernate.Connection.DriverConnectionProvider\"\r\n        \/>\r\n        <add\r\n            key=\"hibernate.dialect\"                     \r\n            value=\"NHibernate.Dialect.MsSql2000Dialect\"\r\n        \/>\r\n        <add\r\n            key=\"hibernate.connection.driver_class\"         \r\n            value=\"NHibernate.Driver.SqlClientDriver\"\r\n        \/>\r\n        <add\r\n            key=\"hibernate.connection.connection_string\"\r\n            value=\"Server=127.0.0.1;initial catalog=UserTest;User id =sa;Password=\"\r\n        \/>\r\n       \r\n    <\/nhibernate>\r\n\r\n3\uff0c\u7f16\u5199\u5b9e\u4f53\u7c7b\r\n\r\n4\uff0c\u7f16\u5199\u5b9e\u4f53\u7c7b\u6620\u5c04\u6587\u4ef6 \u5982\uff1aProduct.hbm.xml\uff0c\u5c06\u6b64\u5176\u5c5e\u6027\u8bbe\u7f6e\u4e3a\u5d4c\u5165\u8d44\u6e90\uff0c\u8fd9\u662f\u56e0\u4e3aNHibernate\u662f\u901a\u8fc7\u67e5\u627e\u7a0b\u5e8f\u96c6\u4e2d\u7684\u8d44\u6e90\u6587\u4ef6\u6765\u8fdb\u884c\u5b9e\u4f53\u7684\u6620\u5c04\uff0c\u6545\u8981\u5c06\u6620\u5c04\u6587\u4ef6\u5d4c\u5165\u5230\u7a0b\u5e8f\u96c6\u4e2d\u3002\r\n\u5982\uff1a<?xml version=\"1.0\" encoding=\"utf-8\" ?> \r\n<hibernate-mapping xmlns=\"urn:nhibernate-mapping-2.0\">\r\n   <class name=\"Web.User\" table=\"users\">\r\n     <id name=\"Id\" column=\"LogonID\" type=\"Int32\" unsaved-value=\"0\">\r\n              <generator class=\"identity\"\/>\r\n     <\/id>\r\n     <property name=\"UserName\" ><\/property>  <!--\u53ef\u4ee5\u4e0d\u6307\u5b9a\u5bf9\u5e94\u7684\u6570\u636e\u5e93\u7684\u5b57\u6bb5\u503c\u53ca\u7c7b\u578b\uff0c\u4f1a\u81ea\u52a8\u636e\u5b9e\u4f53\u7c7b\u521b\u5efa-->\r\n     <property name=\"Password\" column=\"Password\" type=\"String\" ><\/property>\r\n     <property name=\"EmailAddress\" column=\"EmailAddress\" type=\"string\" ><\/property>\r\n     <property name=\"LastLogon\" column=\"LastLogon\" type=\"DateTime\"><\/property>\r\n  <\/class>\r\n\r\n<\/hibernate-mapping>\r\n \u6ce8\uff1a1), \u8981\u4e3aMicrosoft Visual Studio 2008\u6dfb\u52a0\u7f16\u5199NHibernate\u914d\u7f6e\u6587\u4ef6\u667a\u80fd\u63d0\u793a\u7684\u529f\u80fd\u3002\u53ea\u8981\u5728\u4e0b\u8f7d\u7684NHibernate\u91cc\u627e\u5230configuration.xsd\u548c nhibernate-mapping.xsd\u4e24\u4e2a\u6587\u4ef6\u5e76\u590d\u5236\u5230X:\\Program Files\\Microsoft Visual Studio 9.0\\Xml\\Schemas\u76ee\u5f55\u5373\u53ef\u3002\r\n        2),\u6b64\u65f6\u5373\u53ef\u901a\u8fc7\u67b6\u6784\u7c7b\u6765\u81ea\u52a8\u751f\u6210\u6570\u636e\u5e93\u8868\r\n            var cfg = new Configuration();\r\n            cfg.Configure();\r\n            cfg.AddAssembly(typeof(Product).Assembly);\r\n            new SchemaExport(cfg).Execute(true, true, false);\r\n\r\n5\uff0c\u56e0\u4e3a\u64cd\u4f5c\u6570\u636e\u5e93\u662f\u7528Session\u6765\u5b8c\u6210\u7684\uff0c\u53ef\u7f16\u5199\u4e00helper\u7c7b\uff0c\u7528\u6765\u96c6\u4e2d\u5904\u7406, \u7f16\u5199NHibernateHelper.cs\u5982\u4e0b\uff1a\r\nusing NHibernate;\r\nusing NHibernate.Cfg;\r\nusing NHebernateTest.Domain;\r\n\r\n\r\nnamespace NHebernateTest.Repositories\r\n{\r\n\r\n    public class NHibernateHelper\r\n    {\r\n\r\n        private static ISessionFactory _sessionFactory;\r\n\r\n\r\n\r\n        private static ISessionFactory SessionFactory\r\n        {\r\n\r\n            get\r\n            {\r\n\r\n                if (_sessionFactory == null)\r\n                {\r\n\r\n                    var configuration = new Configuration();\r\n\r\n                    configuration.Configure();\r\n\r\n                    \/\/configuration.AddAssembly(typeof(Product).Assembly);\r\n\r\n                    _sessionFactory = configuration.BuildSessionFactory();\r\n\r\n                }\r\n\r\n                return _sessionFactory;\r\n\r\n            }\r\n\r\n        }\r\n\r\n\r\n\r\n        public static ISession OpenSession()\r\n        {\r\n\r\n            return SessionFactory.OpenSession();\r\n\r\n        }\r\n\r\n    }\r\n\r\n}\r\n\r\n\r\n6\uff0c\u7f16\u5199\u64cd\u4f5c\u7c7b\u53ca\u63a5\u53e3\uff0c\u64cd\u4f5c\u7c7b\u5373\u662f\u5f15\u7528\u4e0a\u9762\u7684helper\u7c7b\u8fdb\u884c\u6570\u636e\u5e93\u7684\u64cd\u4f5c\u3002\r\n    1\uff09CRUD\u64cd\u4f5c\uff1a\r\n           using (ISession session = NHibernateHelper.OpenSession())\r\n            using (ITransaction transaction = session.BeginTransaction())\r\n            {\r\n                session.Update(product); \/\/session.Save(product);session.Delete(product);\r\n                transaction.Commit();\r\n            }\r\n     2)\u67e5\u8be2\r\n\r\n    *  \u636e\u5173\u952e\u5b57\u67e5\u8be2\uff1asession.Get<Product>(productId);     \r\n    * \u636e\u5176\u5b83\u5c5e\u6027\u67e5\u8be2\r\n    *      \u8fd4\u56de\u4e00\u4e2a\u503c\uff1asession.CreateCriteria(typeof(Product)).Add(Restrictions.Eq(\"Name\", name)).UniqueResult<Product>();\r\n    *      \u8fd4\u56de\u4e00\u4e2a\u96c6\u5408\uff1asession.CreateCriteria(typeof(Product)).Add(Restrictions.Eq(\"Name\", name)).List<Product>();\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4e0b\u8f7d\uff1anhibernate \u4f7f\u7528\u65b9\u5f0f\uff1a 1\uff0c\u9879\u76ee\u5f15\u5165dll: * Antlr3.Runtime.dll * C [&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-1379","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\/1379","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=1379"}],"version-history":[{"count":0,"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/posts\/1379\/revisions"}],"wp:attachment":[{"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/media?parent=1379"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/categories?post=1379"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/tags?post=1379"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}