NHibernateの基本(簡単なチュートリアル)

codeproject.comのNHibernate Basics記事の翻訳。



運営管理



Nhibernateは、.NETプラットフォームのオブジェクト実装マッピングのソリューションです。 このフレームワークにより、オブジェクト指向モデルを従来のデータベースにマッピングできます。 その主な利点は、.Netクラスのデータベーステーブルへのマッピングと、CLRデータ型からSQL型へのマッピングです。



記事のタイトルに示されているように、データベースからビジネスオブジェクトをロードし、変更されたオブジェクトをデータベースに保存する方法がわかります。



準備する



デモアプリケーションでは、最も簡単な方法としてNHibernateをインストールして使用する方法を示します。 アプリケーションは、Employeeオブジェクトを作成し、それをEmployeeテーブルに保存します。 また、Employeeオブジェクトの取得や削除などの操作も実行します。 このアプリケーションは、NHibernate 3.2.0、VS 2010、およびSQL Server 2008を使用して作成されました。



コードの使用



データベース



まず、データベースをセットアップします。 SQL Server Managment Strudioを使用して、データベースを作成および操作します。 以下に示すように、新しいデータベースを作成し、NHibernateBasicsという名前を付けます。







次に、IDとNAMEの2つの列を持つEmployeeテーブルを追加します。







ID列は主キーである必要があり、自動インクリメントである必要があります。 列のプロパティにID仕様を含めることを忘れないでください。







ビジネスオブジェクト



データベースはデモンストレーションの準備ができています。 Studioを実行し、新しいWindowsFormApplicationアプリケーションを作成すると、プロジェクトはNhibernateBasicsと呼ばれます。

新しいクラスを追加し、Employee.csという名前を付けて、次のコードを貼り付けます。



namespace NHibernateBasics { public class Employee { public virtual int ID { get; set; } public virtual string Name { get; set; } } }
      
      





Nhibernateの強みの1つは、ビジネスクラス用の特別なインターフェイスを必要としないことです。 これらのオブジェクトは、データのロードおよび保存のメカニズムとは無関係です。 ただし、Proxiを作成できるように、クラスプロパティを仮想として記述する必要があります。



XMLファイルのマップ



冬眠用の特定のコードがないため、誰かがデータベースからビジネスオブジェクトへ、またはその逆への変換を担当する必要があります。 この変換は、XMLファイルを介してリンクするか、属性をクラスとプロパティにバインドすることで実現できます。

デモアプリケーションでは、ビジネスオブジェクトクラスが乱雑にならないように、マッピングファイルを使用しました。



新しいXMLファイルをプロジェクトに追加します。 XMLファイルはマッピングファイルとして使用されます。 ファイル名はEmployee.hbm.xmlである必要があります。 クラスファイル
 .cs 
      

.hbm.xml

.



.



<?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="NHibernateBasics" assembly="NHibernateBasics"> <class name="Employee" table="Employee"> <id name="ID" column="ID"> <generator class="identity"/> </id> <property name="Name" column="Name" /> </class> </hibernate-mapping>






XML Build Action = Embedded Resource.











(app.config).

.



<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" /> </configSections> <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2"> <session-factory> <property name="connection.provider"> NHibernate.Connection.DriverConnectionProvider</property> <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property> <property name="query.substitutions">hqlFunction=SQLFUNC</property> <property name="connection.driver_class"> NHibernate.Driver.SqlClientDriver</property> <property name="connection.connection_string"> Data Source=(Local)\SQL2008;Initial Catalog=NHibernateBasics; Integrated Security=True</property> <property name="show_sql">true</property> <mapping assembly="NHibernateBasics" /> </session-factory> </hibernate-configuration> </configuration>





Connection-string . CATALOG=, NHibernateBasics. mapping assembly=, NHibernateBasics.











. , .



:



using(mySession.BeginTransaction()) { // Insert two employees in Database mySession.Save(myInitialObjects[0]); mySession.Save(myInitialObjects[1]); mySession.Transaction.Commit(); }






:



using(mySession.BeginTransaction()) { // ICriteria criteria = mySession.CreateCriteria<employee>(); IList<employee> list = criteria.List<employee>(); for (int i = 0; i < myFinalObjects.Length; i++) { myFinalObjects[i] = list[i]; MessageBox.Show("ID: " + myFinalObjects[i].ID + " Name: " + myFinalObjects[i].Name); } mySession.Transaction.Commit(); }





:



StringBuilder messageString = new StringBuilder(); // for (int i = 0; i < 2; i++) { messageString.AppendLine("Comparing Class Object " + myInitialObjects[i].Name + " and DB Object " + myFinalObjects[i].Name + ". Result = " + myInitialObjects[i].Equals(myFinalObjects[i]).ToString()); } MessageBox.Show(messageString.ToString());





:



using (mySession.BeginTransaction()) { // Delete one object from Database mySession.Delete(myInitialObjects[0]); mySession.Transaction.Commit(); }





:



using (mySession.BeginTransaction()) { ICriteria criteria = mySession.CreateCriteria<employee>(); IList<employee> list = criteria.List<employee>(); StringBuilder messageString = new StringBuilder(); // Load and display the data foreach (Employee employee in list) { messageString.AppendLine("ID: " + employee.ID + " Name: " + employee.Name); } MessageBox.Show(messageString.ToString()); }





NHibernte . , .



Nhibernate.



Happy coding /////



: NHibernateBasics.zip



Nhiberante. Nhibernate ?
















.cs

.hbm.xml

.



.



<?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="NHibernateBasics" assembly="NHibernateBasics"> <class name="Employee" table="Employee"> <id name="ID" column="ID"> <generator class="identity"/> </id> <property name="Name" column="Name" /> </class> </hibernate-mapping>






XML Build Action = Embedded Resource.











(app.config).

.



<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" /> </configSections> <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2"> <session-factory> <property name="connection.provider"> NHibernate.Connection.DriverConnectionProvider</property> <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property> <property name="query.substitutions">hqlFunction=SQLFUNC</property> <property name="connection.driver_class"> NHibernate.Driver.SqlClientDriver</property> <property name="connection.connection_string"> Data Source=(Local)\SQL2008;Initial Catalog=NHibernateBasics; Integrated Security=True</property> <property name="show_sql">true</property> <mapping assembly="NHibernateBasics" /> </session-factory> </hibernate-configuration> </configuration>





Connection-string . CATALOG=, NHibernateBasics. mapping assembly=, NHibernateBasics.











. , .



:



using(mySession.BeginTransaction()) { // Insert two employees in Database mySession.Save(myInitialObjects[0]); mySession.Save(myInitialObjects[1]); mySession.Transaction.Commit(); }






:



using(mySession.BeginTransaction()) { // ICriteria criteria = mySession.CreateCriteria<employee>(); IList<employee> list = criteria.List<employee>(); for (int i = 0; i < myFinalObjects.Length; i++) { myFinalObjects[i] = list[i]; MessageBox.Show("ID: " + myFinalObjects[i].ID + " Name: " + myFinalObjects[i].Name); } mySession.Transaction.Commit(); }





:



StringBuilder messageString = new StringBuilder(); // for (int i = 0; i < 2; i++) { messageString.AppendLine("Comparing Class Object " + myInitialObjects[i].Name + " and DB Object " + myFinalObjects[i].Name + ". Result = " + myInitialObjects[i].Equals(myFinalObjects[i]).ToString()); } MessageBox.Show(messageString.ToString());





:



using (mySession.BeginTransaction()) { // Delete one object from Database mySession.Delete(myInitialObjects[0]); mySession.Transaction.Commit(); }





:



using (mySession.BeginTransaction()) { ICriteria criteria = mySession.CreateCriteria<employee>(); IList<employee> list = criteria.List<employee>(); StringBuilder messageString = new StringBuilder(); // Load and display the data foreach (Employee employee in list) { messageString.AppendLine("ID: " + employee.ID + " Name: " + employee.Name); } MessageBox.Show(messageString.ToString()); }





NHibernte . , .



Nhibernate.



Happy coding /////



: NHibernateBasics.zip



Nhiberante. Nhibernate ?
















 .cs 
      

.hbm.xml

.



.



<?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="NHibernateBasics" assembly="NHibernateBasics"> <class name="Employee" table="Employee"> <id name="ID" column="ID"> <generator class="identity"/> </id> <property name="Name" column="Name" /> </class> </hibernate-mapping>






XML Build Action = Embedded Resource.











(app.config).

.



<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" /> </configSections> <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2"> <session-factory> <property name="connection.provider"> NHibernate.Connection.DriverConnectionProvider</property> <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property> <property name="query.substitutions">hqlFunction=SQLFUNC</property> <property name="connection.driver_class"> NHibernate.Driver.SqlClientDriver</property> <property name="connection.connection_string"> Data Source=(Local)\SQL2008;Initial Catalog=NHibernateBasics; Integrated Security=True</property> <property name="show_sql">true</property> <mapping assembly="NHibernateBasics" /> </session-factory> </hibernate-configuration> </configuration>





Connection-string . CATALOG=, NHibernateBasics. mapping assembly=, NHibernateBasics.











. , .



:



using(mySession.BeginTransaction()) { // Insert two employees in Database mySession.Save(myInitialObjects[0]); mySession.Save(myInitialObjects[1]); mySession.Transaction.Commit(); }






:



using(mySession.BeginTransaction()) { // ICriteria criteria = mySession.CreateCriteria<employee>(); IList<employee> list = criteria.List<employee>(); for (int i = 0; i < myFinalObjects.Length; i++) { myFinalObjects[i] = list[i]; MessageBox.Show("ID: " + myFinalObjects[i].ID + " Name: " + myFinalObjects[i].Name); } mySession.Transaction.Commit(); }





:



StringBuilder messageString = new StringBuilder(); // for (int i = 0; i < 2; i++) { messageString.AppendLine("Comparing Class Object " + myInitialObjects[i].Name + " and DB Object " + myFinalObjects[i].Name + ". Result = " + myInitialObjects[i].Equals(myFinalObjects[i]).ToString()); } MessageBox.Show(messageString.ToString());





:



using (mySession.BeginTransaction()) { // Delete one object from Database mySession.Delete(myInitialObjects[0]); mySession.Transaction.Commit(); }





:



using (mySession.BeginTransaction()) { ICriteria criteria = mySession.CreateCriteria<employee>(); IList<employee> list = criteria.List<employee>(); StringBuilder messageString = new StringBuilder(); // Load and display the data foreach (Employee employee in list) { messageString.AppendLine("ID: " + employee.ID + " Name: " + employee.Name); } MessageBox.Show(messageString.ToString()); }





NHibernte . , .



Nhibernate.



Happy coding /////



: NHibernateBasics.zip



Nhiberante. Nhibernate ?
















.cs

.hbm.xml

.



.



<?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="NHibernateBasics" assembly="NHibernateBasics"> <class name="Employee" table="Employee"> <id name="ID" column="ID"> <generator class="identity"/> </id> <property name="Name" column="Name" /> </class> </hibernate-mapping>






XML Build Action = Embedded Resource.











(app.config).

.



<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" /> </configSections> <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2"> <session-factory> <property name="connection.provider"> NHibernate.Connection.DriverConnectionProvider</property> <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property> <property name="query.substitutions">hqlFunction=SQLFUNC</property> <property name="connection.driver_class"> NHibernate.Driver.SqlClientDriver</property> <property name="connection.connection_string"> Data Source=(Local)\SQL2008;Initial Catalog=NHibernateBasics; Integrated Security=True</property> <property name="show_sql">true</property> <mapping assembly="NHibernateBasics" /> </session-factory> </hibernate-configuration> </configuration>





Connection-string . CATALOG=, NHibernateBasics. mapping assembly=, NHibernateBasics.











. , .



:



using(mySession.BeginTransaction()) { // Insert two employees in Database mySession.Save(myInitialObjects[0]); mySession.Save(myInitialObjects[1]); mySession.Transaction.Commit(); }






:



using(mySession.BeginTransaction()) { // ICriteria criteria = mySession.CreateCriteria<employee>(); IList<employee> list = criteria.List<employee>(); for (int i = 0; i < myFinalObjects.Length; i++) { myFinalObjects[i] = list[i]; MessageBox.Show("ID: " + myFinalObjects[i].ID + " Name: " + myFinalObjects[i].Name); } mySession.Transaction.Commit(); }





:



StringBuilder messageString = new StringBuilder(); // for (int i = 0; i < 2; i++) { messageString.AppendLine("Comparing Class Object " + myInitialObjects[i].Name + " and DB Object " + myFinalObjects[i].Name + ". Result = " + myInitialObjects[i].Equals(myFinalObjects[i]).ToString()); } MessageBox.Show(messageString.ToString());





:



using (mySession.BeginTransaction()) { // Delete one object from Database mySession.Delete(myInitialObjects[0]); mySession.Transaction.Commit(); }





:



using (mySession.BeginTransaction()) { ICriteria criteria = mySession.CreateCriteria<employee>(); IList<employee> list = criteria.List<employee>(); StringBuilder messageString = new StringBuilder(); // Load and display the data foreach (Employee employee in list) { messageString.AppendLine("ID: " + employee.ID + " Name: " + employee.Name); } MessageBox.Show(messageString.ToString()); }





NHibernte . , .



Nhibernate.



Happy coding /////



: NHibernateBasics.zip



Nhiberante. Nhibernate ?
















 .cs 
      

.hbm.xml

.



.



<?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="NHibernateBasics" assembly="NHibernateBasics"> <class name="Employee" table="Employee"> <id name="ID" column="ID"> <generator class="identity"/> </id> <property name="Name" column="Name" /> </class> </hibernate-mapping>






XML Build Action = Embedded Resource.











(app.config).

.



<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" /> </configSections> <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2"> <session-factory> <property name="connection.provider"> NHibernate.Connection.DriverConnectionProvider</property> <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property> <property name="query.substitutions">hqlFunction=SQLFUNC</property> <property name="connection.driver_class"> NHibernate.Driver.SqlClientDriver</property> <property name="connection.connection_string"> Data Source=(Local)\SQL2008;Initial Catalog=NHibernateBasics; Integrated Security=True</property> <property name="show_sql">true</property> <mapping assembly="NHibernateBasics" /> </session-factory> </hibernate-configuration> </configuration>





Connection-string . CATALOG=, NHibernateBasics. mapping assembly=, NHibernateBasics.







. , .



:



using(mySession.BeginTransaction()) { // Insert two employees in Database mySession.Save(myInitialObjects[0]); mySession.Save(myInitialObjects[1]); mySession.Transaction.Commit(); }

:



using(mySession.BeginTransaction()) { // ICriteria criteria = mySession.CreateCriteria<employee>(); IList<employee> list = criteria.List<employee>(); for (int i = 0; i < myFinalObjects.Length; i++) { myFinalObjects[i] = list[i]; MessageBox.Show("ID: " + myFinalObjects[i].ID + " Name: " + myFinalObjects[i].Name); } mySession.Transaction.Commit(); }





:



StringBuilder messageString = new StringBuilder(); // for (int i = 0; i < 2; i++) { messageString.AppendLine("Comparing Class Object " + myInitialObjects[i].Name + " and DB Object " + myFinalObjects[i].Name + ". Result = " + myInitialObjects[i].Equals(myFinalObjects[i]).ToString()); } MessageBox.Show(messageString.ToString());





:



using (mySession.BeginTransaction()) { // Delete one object from Database mySession.Delete(myInitialObjects[0]); mySession.Transaction.Commit(); }





:



using (mySession.BeginTransaction()) { ICriteria criteria = mySession.CreateCriteria<employee>(); IList<employee> list = criteria.List<employee>(); StringBuilder messageString = new StringBuilder(); // Load and display the data foreach (Employee employee in list) { messageString.AppendLine("ID: " + employee.ID + " Name: " + employee.Name); } MessageBox.Show(messageString.ToString()); }





NHibernte . , .



Nhibernate.



Happy coding /////



: NHibernateBasics.zip



Nhiberante. Nhibernate ?
























.cs

.hbm.xml

.



.



<?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="NHibernateBasics" assembly="NHibernateBasics"> <class name="Employee" table="Employee"> <id name="ID" column="ID"> <generator class="identity"/> </id> <property name="Name" column="Name" /> </class> </hibernate-mapping>






XML Build Action = Embedded Resource.











(app.config).

.



<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" /> </configSections> <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2"> <session-factory> <property name="connection.provider"> NHibernate.Connection.DriverConnectionProvider</property> <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property> <property name="query.substitutions">hqlFunction=SQLFUNC</property> <property name="connection.driver_class"> NHibernate.Driver.SqlClientDriver</property> <property name="connection.connection_string"> Data Source=(Local)\SQL2008;Initial Catalog=NHibernateBasics; Integrated Security=True</property> <property name="show_sql">true</property> <mapping assembly="NHibernateBasics" /> </session-factory> </hibernate-configuration> </configuration>





Connection-string . CATALOG=, NHibernateBasics. mapping assembly=, NHibernateBasics.











. , .



:



using(mySession.BeginTransaction()) { // Insert two employees in Database mySession.Save(myInitialObjects[0]); mySession.Save(myInitialObjects[1]); mySession.Transaction.Commit(); }

:



using(mySession.BeginTransaction()) { // ICriteria criteria = mySession.CreateCriteria<employee>(); IList<employee> list = criteria.List<employee>(); for (int i = 0; i < myFinalObjects.Length; i++) { myFinalObjects[i] = list[i]; MessageBox.Show("ID: " + myFinalObjects[i].ID + " Name: " + myFinalObjects[i].Name); } mySession.Transaction.Commit(); }





:



StringBuilder messageString = new StringBuilder(); // for (int i = 0; i < 2; i++) { messageString.AppendLine("Comparing Class Object " + myInitialObjects[i].Name + " and DB Object " + myFinalObjects[i].Name + ". Result = " + myInitialObjects[i].Equals(myFinalObjects[i]).ToString()); } MessageBox.Show(messageString.ToString());





:



using (mySession.BeginTransaction()) { // Delete one object from Database mySession.Delete(myInitialObjects[0]); mySession.Transaction.Commit(); }





:



using (mySession.BeginTransaction()) { ICriteria criteria = mySession.CreateCriteria<employee>(); IList<employee> list = criteria.List<employee>(); StringBuilder messageString = new StringBuilder(); // Load and display the data foreach (Employee employee in list) { messageString.AppendLine("ID: " + employee.ID + " Name: " + employee.Name); } MessageBox.Show(messageString.ToString()); }





NHibernte . , .



Nhibernate.



Happy coding /////



: NHibernateBasics.zip



Nhiberante. Nhibernate ?




















.cs

.hbm.xml

.



.



<?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="NHibernateBasics" assembly="NHibernateBasics"> <class name="Employee" table="Employee"> <id name="ID" column="ID"> <generator class="identity"/> </id> <property name="Name" column="Name" /> </class> </hibernate-mapping>






XML Build Action = Embedded Resource.











(app.config).

.



<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" /> </configSections> <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2"> <session-factory> <property name="connection.provider"> NHibernate.Connection.DriverConnectionProvider</property> <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property> <property name="query.substitutions">hqlFunction=SQLFUNC</property> <property name="connection.driver_class"> NHibernate.Driver.SqlClientDriver</property> <property name="connection.connection_string"> Data Source=(Local)\SQL2008;Initial Catalog=NHibernateBasics; Integrated Security=True</property> <property name="show_sql">true</property> <mapping assembly="NHibernateBasics" /> </session-factory> </hibernate-configuration> </configuration>





Connection-string . CATALOG=, NHibernateBasics. mapping assembly=, NHibernateBasics.











. , .



:



using(mySession.BeginTransaction()) { // Insert two employees in Database mySession.Save(myInitialObjects[0]); mySession.Save(myInitialObjects[1]); mySession.Transaction.Commit(); }






:



using(mySession.BeginTransaction()) { // ICriteria criteria = mySession.CreateCriteria<employee>(); IList<employee> list = criteria.List<employee>(); for (int i = 0; i < myFinalObjects.Length; i++) { myFinalObjects[i] = list[i]; MessageBox.Show("ID: " + myFinalObjects[i].ID + " Name: " + myFinalObjects[i].Name); } mySession.Transaction.Commit(); }





:



StringBuilder messageString = new StringBuilder(); // for (int i = 0; i < 2; i++) { messageString.AppendLine("Comparing Class Object " + myInitialObjects[i].Name + " and DB Object " + myFinalObjects[i].Name + ". Result = " + myInitialObjects[i].Equals(myFinalObjects[i]).ToString()); } MessageBox.Show(messageString.ToString());





:



using (mySession.BeginTransaction()) { // Delete one object from Database mySession.Delete(myInitialObjects[0]); mySession.Transaction.Commit(); }





:



using (mySession.BeginTransaction()) { ICriteria criteria = mySession.CreateCriteria<employee>(); IList<employee> list = criteria.List<employee>(); StringBuilder messageString = new StringBuilder(); // Load and display the data foreach (Employee employee in list) { messageString.AppendLine("ID: " + employee.ID + " Name: " + employee.Name); } MessageBox.Show(messageString.ToString()); }





NHibernte . , .



Nhibernate.



Happy coding /////



: NHibernateBasics.zip



Nhiberante. Nhibernate ?
















.cs

.hbm.xml

.



.



<?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="NHibernateBasics" assembly="NHibernateBasics"> <class name="Employee" table="Employee"> <id name="ID" column="ID"> <generator class="identity"/> </id> <property name="Name" column="Name" /> </class> </hibernate-mapping>






XML Build Action = Embedded Resource.











(app.config).

.



<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" /> </configSections> <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2"> <session-factory> <property name="connection.provider"> NHibernate.Connection.DriverConnectionProvider</property> <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property> <property name="query.substitutions">hqlFunction=SQLFUNC</property> <property name="connection.driver_class"> NHibernate.Driver.SqlClientDriver</property> <property name="connection.connection_string"> Data Source=(Local)\SQL2008;Initial Catalog=NHibernateBasics; Integrated Security=True</property> <property name="show_sql">true</property> <mapping assembly="NHibernateBasics" /> </session-factory> </hibernate-configuration> </configuration>





Connection-string . CATALOG=, NHibernateBasics. mapping assembly=, NHibernateBasics.











. , .



:



using(mySession.BeginTransaction()) { // Insert two employees in Database mySession.Save(myInitialObjects[0]); mySession.Save(myInitialObjects[1]); mySession.Transaction.Commit(); }






:



using(mySession.BeginTransaction()) { // ICriteria criteria = mySession.CreateCriteria<employee>(); IList<employee> list = criteria.List<employee>(); for (int i = 0; i < myFinalObjects.Length; i++) { myFinalObjects[i] = list[i]; MessageBox.Show("ID: " + myFinalObjects[i].ID + " Name: " + myFinalObjects[i].Name); } mySession.Transaction.Commit(); }





:



StringBuilder messageString = new StringBuilder(); // for (int i = 0; i < 2; i++) { messageString.AppendLine("Comparing Class Object " + myInitialObjects[i].Name + " and DB Object " + myFinalObjects[i].Name + ". Result = " + myInitialObjects[i].Equals(myFinalObjects[i]).ToString()); } MessageBox.Show(messageString.ToString());





:



using (mySession.BeginTransaction()) { // Delete one object from Database mySession.Delete(myInitialObjects[0]); mySession.Transaction.Commit(); }





:



using (mySession.BeginTransaction()) { ICriteria criteria = mySession.CreateCriteria<employee>(); IList<employee> list = criteria.List<employee>(); StringBuilder messageString = new StringBuilder(); // Load and display the data foreach (Employee employee in list) { messageString.AppendLine("ID: " + employee.ID + " Name: " + employee.Name); } MessageBox.Show(messageString.ToString()); }





NHibernte . , .



Nhibernate.



Happy coding /////



: NHibernateBasics.zip



Nhiberante. Nhibernate ?
















 .cs 
      

.hbm.xml

.



.



<?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="NHibernateBasics" assembly="NHibernateBasics"> <class name="Employee" table="Employee"> <id name="ID" column="ID"> <generator class="identity"/> </id> <property name="Name" column="Name" /> </class> </hibernate-mapping>






XML Build Action = Embedded Resource.











(app.config).

.



<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" /> </configSections> <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2"> <session-factory> <property name="connection.provider"> NHibernate.Connection.DriverConnectionProvider</property> <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property> <property name="query.substitutions">hqlFunction=SQLFUNC</property> <property name="connection.driver_class"> NHibernate.Driver.SqlClientDriver</property> <property name="connection.connection_string"> Data Source=(Local)\SQL2008;Initial Catalog=NHibernateBasics; Integrated Security=True</property> <property name="show_sql">true</property> <mapping assembly="NHibernateBasics" /> </session-factory> </hibernate-configuration> </configuration>





Connection-string . CATALOG=, NHibernateBasics. mapping assembly=, NHibernateBasics.











. , .



:



using(mySession.BeginTransaction()) { // Insert two employees in Database mySession.Save(myInitialObjects[0]); mySession.Save(myInitialObjects[1]); mySession.Transaction.Commit(); }






:



using(mySession.BeginTransaction()) { // ICriteria criteria = mySession.CreateCriteria<employee>(); IList<employee> list = criteria.List<employee>(); for (int i = 0; i < myFinalObjects.Length; i++) { myFinalObjects[i] = list[i]; MessageBox.Show("ID: " + myFinalObjects[i].ID + " Name: " + myFinalObjects[i].Name); } mySession.Transaction.Commit(); }





:



StringBuilder messageString = new StringBuilder(); // for (int i = 0; i < 2; i++) { messageString.AppendLine("Comparing Class Object " + myInitialObjects[i].Name + " and DB Object " + myFinalObjects[i].Name + ". Result = " + myInitialObjects[i].Equals(myFinalObjects[i]).ToString()); } MessageBox.Show(messageString.ToString());





:



using (mySession.BeginTransaction()) { // Delete one object from Database mySession.Delete(myInitialObjects[0]); mySession.Transaction.Commit(); }





:



using (mySession.BeginTransaction()) { ICriteria criteria = mySession.CreateCriteria<employee>(); IList<employee> list = criteria.List<employee>(); StringBuilder messageString = new StringBuilder(); // Load and display the data foreach (Employee employee in list) { messageString.AppendLine("ID: " + employee.ID + " Name: " + employee.Name); } MessageBox.Show(messageString.ToString()); }





NHibernte . , .



Nhibernate.



Happy coding /////



: NHibernateBasics.zip



Nhiberante. Nhibernate ?
















.cs

.hbm.xml

.



.



<?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="NHibernateBasics" assembly="NHibernateBasics"> <class name="Employee" table="Employee"> <id name="ID" column="ID"> <generator class="identity"/> </id> <property name="Name" column="Name" /> </class> </hibernate-mapping>






XML Build Action = Embedded Resource.











(app.config).

.



<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" /> </configSections> <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2"> <session-factory> <property name="connection.provider"> NHibernate.Connection.DriverConnectionProvider</property> <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property> <property name="query.substitutions">hqlFunction=SQLFUNC</property> <property name="connection.driver_class"> NHibernate.Driver.SqlClientDriver</property> <property name="connection.connection_string"> Data Source=(Local)\SQL2008;Initial Catalog=NHibernateBasics; Integrated Security=True</property> <property name="show_sql">true</property> <mapping assembly="NHibernateBasics" /> </session-factory> </hibernate-configuration> </configuration>





Connection-string . CATALOG=, NHibernateBasics. mapping assembly=, NHibernateBasics.











. , .



:



using(mySession.BeginTransaction()) { // Insert two employees in Database mySession.Save(myInitialObjects[0]); mySession.Save(myInitialObjects[1]); mySession.Transaction.Commit(); }






:



using(mySession.BeginTransaction()) { // ICriteria criteria = mySession.CreateCriteria<employee>(); IList<employee> list = criteria.List<employee>(); for (int i = 0; i < myFinalObjects.Length; i++) { myFinalObjects[i] = list[i]; MessageBox.Show("ID: " + myFinalObjects[i].ID + " Name: " + myFinalObjects[i].Name); } mySession.Transaction.Commit(); }





:



StringBuilder messageString = new StringBuilder(); // for (int i = 0; i < 2; i++) { messageString.AppendLine("Comparing Class Object " + myInitialObjects[i].Name + " and DB Object " + myFinalObjects[i].Name + ". Result = " + myInitialObjects[i].Equals(myFinalObjects[i]).ToString()); } MessageBox.Show(messageString.ToString());





:



using (mySession.BeginTransaction()) { // Delete one object from Database mySession.Delete(myInitialObjects[0]); mySession.Transaction.Commit(); }





:



using (mySession.BeginTransaction()) { ICriteria criteria = mySession.CreateCriteria<employee>(); IList<employee> list = criteria.List<employee>(); StringBuilder messageString = new StringBuilder(); // Load and display the data foreach (Employee employee in list) { messageString.AppendLine("ID: " + employee.ID + " Name: " + employee.Name); } MessageBox.Show(messageString.ToString()); }





NHibernte . , .



Nhibernate.



Happy coding /////



: NHibernateBasics.zip



Nhiberante. Nhibernate ?
















 .cs 
      

.hbm.xml

.



.



<?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="NHibernateBasics" assembly="NHibernateBasics"> <class name="Employee" table="Employee"> <id name="ID" column="ID"> <generator class="identity"/> </id> <property name="Name" column="Name" /> </class> </hibernate-mapping>






XML Build Action = Embedded Resource.











(app.config).

.



<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" /> </configSections> <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2"> <session-factory> <property name="connection.provider"> NHibernate.Connection.DriverConnectionProvider</property> <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property> <property name="query.substitutions">hqlFunction=SQLFUNC</property> <property name="connection.driver_class"> NHibernate.Driver.SqlClientDriver</property> <property name="connection.connection_string"> Data Source=(Local)\SQL2008;Initial Catalog=NHibernateBasics; Integrated Security=True</property> <property name="show_sql">true</property> <mapping assembly="NHibernateBasics" /> </session-factory> </hibernate-configuration> </configuration>





Connection-string . CATALOG=, NHibernateBasics. mapping assembly=, NHibernateBasics.











. , .



:



using(mySession.BeginTransaction()) { // Insert two employees in Database mySession.Save(myInitialObjects[0]); mySession.Save(myInitialObjects[1]); mySession.Transaction.Commit(); }






:



using(mySession.BeginTransaction()) { // ICriteria criteria = mySession.CreateCriteria<employee>(); IList<employee> list = criteria.List<employee>(); for (int i = 0; i < myFinalObjects.Length; i++) { myFinalObjects[i] = list[i]; MessageBox.Show("ID: " + myFinalObjects[i].ID + " Name: " + myFinalObjects[i].Name); } mySession.Transaction.Commit(); }





:



StringBuilder messageString = new StringBuilder(); // for (int i = 0; i < 2; i++) { messageString.AppendLine("Comparing Class Object " + myInitialObjects[i].Name + " and DB Object " + myFinalObjects[i].Name + ". Result = " + myInitialObjects[i].Equals(myFinalObjects[i]).ToString()); } MessageBox.Show(messageString.ToString());





:



using (mySession.BeginTransaction()) { // Delete one object from Database mySession.Delete(myInitialObjects[0]); mySession.Transaction.Commit(); }





:



using (mySession.BeginTransaction()) { ICriteria criteria = mySession.CreateCriteria<employee>(); IList<employee> list = criteria.List<employee>(); StringBuilder messageString = new StringBuilder(); // Load and display the data foreach (Employee employee in list) { messageString.AppendLine("ID: " + employee.ID + " Name: " + employee.Name); } MessageBox.Show(messageString.ToString()); }





NHibernte . , .



Nhibernate.



Happy coding /////



: NHibernateBasics.zip



Nhiberante. Nhibernate ?
















.cs

.hbm.xml

.



.



<?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="NHibernateBasics" assembly="NHibernateBasics"> <class name="Employee" table="Employee"> <id name="ID" column="ID"> <generator class="identity"/> </id> <property name="Name" column="Name" /> </class> </hibernate-mapping>






XML Build Action = Embedded Resource.











(app.config).

.



<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" /> </configSections> <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2"> <session-factory> <property name="connection.provider"> NHibernate.Connection.DriverConnectionProvider</property> <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property> <property name="query.substitutions">hqlFunction=SQLFUNC</property> <property name="connection.driver_class"> NHibernate.Driver.SqlClientDriver</property> <property name="connection.connection_string"> Data Source=(Local)\SQL2008;Initial Catalog=NHibernateBasics; Integrated Security=True</property> <property name="show_sql">true</property> <mapping assembly="NHibernateBasics" /> </session-factory> </hibernate-configuration> </configuration>





Connection-string . CATALOG=, NHibernateBasics. mapping assembly=, NHibernateBasics.











. , .



:



using(mySession.BeginTransaction()) { // Insert two employees in Database mySession.Save(myInitialObjects[0]); mySession.Save(myInitialObjects[1]); mySession.Transaction.Commit(); }






:



using(mySession.BeginTransaction()) { // ICriteria criteria = mySession.CreateCriteria<employee>(); IList<employee> list = criteria.List<employee>(); for (int i = 0; i < myFinalObjects.Length; i++) { myFinalObjects[i] = list[i]; MessageBox.Show("ID: " + myFinalObjects[i].ID + " Name: " + myFinalObjects[i].Name); } mySession.Transaction.Commit(); }





:



StringBuilder messageString = new StringBuilder(); // for (int i = 0; i < 2; i++) { messageString.AppendLine("Comparing Class Object " + myInitialObjects[i].Name + " and DB Object " + myFinalObjects[i].Name + ". Result = " + myInitialObjects[i].Equals(myFinalObjects[i]).ToString()); } MessageBox.Show(messageString.ToString());





:



using (mySession.BeginTransaction()) { // Delete one object from Database mySession.Delete(myInitialObjects[0]); mySession.Transaction.Commit(); }





:



using (mySession.BeginTransaction()) { ICriteria criteria = mySession.CreateCriteria<employee>(); IList<employee> list = criteria.List<employee>(); StringBuilder messageString = new StringBuilder(); // Load and display the data foreach (Employee employee in list) { messageString.AppendLine("ID: " + employee.ID + " Name: " + employee.Name); } MessageBox.Show(messageString.ToString()); }





NHibernte . , .



Nhibernate.



Happy coding /////



: NHibernateBasics.zip



Nhiberante. Nhibernate ?
















 .cs 
      

.hbm.xml

.



.



<?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="NHibernateBasics" assembly="NHibernateBasics"> <class name="Employee" table="Employee"> <id name="ID" column="ID"> <generator class="identity"/> </id> <property name="Name" column="Name" /> </class> </hibernate-mapping>






XML Build Action = Embedded Resource.











(app.config).

.



<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" /> </configSections> <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2"> <session-factory> <property name="connection.provider"> NHibernate.Connection.DriverConnectionProvider</property> <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property> <property name="query.substitutions">hqlFunction=SQLFUNC</property> <property name="connection.driver_class"> NHibernate.Driver.SqlClientDriver</property> <property name="connection.connection_string"> Data Source=(Local)\SQL2008;Initial Catalog=NHibernateBasics; Integrated Security=True</property> <property name="show_sql">true</property> <mapping assembly="NHibernateBasics" /> </session-factory> </hibernate-configuration> </configuration>





Connection-string . CATALOG=, NHibernateBasics. mapping assembly=, NHibernateBasics.











. , .



:



using(mySession.BeginTransaction()) { // Insert two employees in Database mySession.Save(myInitialObjects[0]); mySession.Save(myInitialObjects[1]); mySession.Transaction.Commit(); }






:



using(mySession.BeginTransaction()) { // ICriteria criteria = mySession.CreateCriteria<employee>(); IList<employee> list = criteria.List<employee>(); for (int i = 0; i < myFinalObjects.Length; i++) { myFinalObjects[i] = list[i]; MessageBox.Show("ID: " + myFinalObjects[i].ID + " Name: " + myFinalObjects[i].Name); } mySession.Transaction.Commit(); }





:



StringBuilder messageString = new StringBuilder(); // for (int i = 0; i < 2; i++) { messageString.AppendLine("Comparing Class Object " + myInitialObjects[i].Name + " and DB Object " + myFinalObjects[i].Name + ". Result = " + myInitialObjects[i].Equals(myFinalObjects[i]).ToString()); } MessageBox.Show(messageString.ToString());





:



using (mySession.BeginTransaction()) { // Delete one object from Database mySession.Delete(myInitialObjects[0]); mySession.Transaction.Commit(); }





:



using (mySession.BeginTransaction()) { ICriteria criteria = mySession.CreateCriteria<employee>(); IList<employee> list = criteria.List<employee>(); StringBuilder messageString = new StringBuilder(); // Load and display the data foreach (Employee employee in list) { messageString.AppendLine("ID: " + employee.ID + " Name: " + employee.Name); } MessageBox.Show(messageString.ToString()); }





NHibernte . , .



Nhibernate.



Happy coding /////



: NHibernateBasics.zip



Nhiberante. Nhibernate ?
















.cs

.hbm.xml

.



.



<?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="NHibernateBasics" assembly="NHibernateBasics"> <class name="Employee" table="Employee"> <id name="ID" column="ID"> <generator class="identity"/> </id> <property name="Name" column="Name" /> </class> </hibernate-mapping>






XML Build Action = Embedded Resource.











(app.config).

.



<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" /> </configSections> <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2"> <session-factory> <property name="connection.provider"> NHibernate.Connection.DriverConnectionProvider</property> <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property> <property name="query.substitutions">hqlFunction=SQLFUNC</property> <property name="connection.driver_class"> NHibernate.Driver.SqlClientDriver</property> <property name="connection.connection_string"> Data Source=(Local)\SQL2008;Initial Catalog=NHibernateBasics; Integrated Security=True</property> <property name="show_sql">true</property> <mapping assembly="NHibernateBasics" /> </session-factory> </hibernate-configuration> </configuration>





Connection-string . CATALOG=, NHibernateBasics. mapping assembly=, NHibernateBasics.











. , .



:



using(mySession.BeginTransaction()) { // Insert two employees in Database mySession.Save(myInitialObjects[0]); mySession.Save(myInitialObjects[1]); mySession.Transaction.Commit(); }






:



using(mySession.BeginTransaction()) { // ICriteria criteria = mySession.CreateCriteria<employee>(); IList<employee> list = criteria.List<employee>(); for (int i = 0; i < myFinalObjects.Length; i++) { myFinalObjects[i] = list[i]; MessageBox.Show("ID: " + myFinalObjects[i].ID + " Name: " + myFinalObjects[i].Name); } mySession.Transaction.Commit(); }





:



StringBuilder messageString = new StringBuilder(); // for (int i = 0; i < 2; i++) { messageString.AppendLine("Comparing Class Object " + myInitialObjects[i].Name + " and DB Object " + myFinalObjects[i].Name + ". Result = " + myInitialObjects[i].Equals(myFinalObjects[i]).ToString()); } MessageBox.Show(messageString.ToString());





:



using (mySession.BeginTransaction()) { // Delete one object from Database mySession.Delete(myInitialObjects[0]); mySession.Transaction.Commit(); }





:



using (mySession.BeginTransaction()) { ICriteria criteria = mySession.CreateCriteria<employee>(); IList<employee> list = criteria.List<employee>(); StringBuilder messageString = new StringBuilder(); // Load and display the data foreach (Employee employee in list) { messageString.AppendLine("ID: " + employee.ID + " Name: " + employee.Name); } MessageBox.Show(messageString.ToString()); }





NHibernte . , .



Nhibernate.



Happy coding /////



: NHibernateBasics.zip



Nhiberante. Nhibernate ?
















 .cs 
      

.hbm.xml

.



.



<?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="NHibernateBasics" assembly="NHibernateBasics"> <class name="Employee" table="Employee"> <id name="ID" column="ID"> <generator class="identity"/> </id> <property name="Name" column="Name" /> </class> </hibernate-mapping>






XML Build Action = Embedded Resource.











(app.config).

.



<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" /> </configSections> <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2"> <session-factory> <property name="connection.provider"> NHibernate.Connection.DriverConnectionProvider</property> <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property> <property name="query.substitutions">hqlFunction=SQLFUNC</property> <property name="connection.driver_class"> NHibernate.Driver.SqlClientDriver</property> <property name="connection.connection_string"> Data Source=(Local)\SQL2008;Initial Catalog=NHibernateBasics; Integrated Security=True</property> <property name="show_sql">true</property> <mapping assembly="NHibernateBasics" /> </session-factory> </hibernate-configuration> </configuration>





Connection-string . CATALOG=, NHibernateBasics. mapping assembly=, NHibernateBasics.











. , .



:



using(mySession.BeginTransaction()) { // Insert two employees in Database mySession.Save(myInitialObjects[0]); mySession.Save(myInitialObjects[1]); mySession.Transaction.Commit(); }






:



using(mySession.BeginTransaction()) { // ICriteria criteria = mySession.CreateCriteria<employee>(); IList<employee> list = criteria.List<employee>(); for (int i = 0; i < myFinalObjects.Length; i++) { myFinalObjects[i] = list[i]; MessageBox.Show("ID: " + myFinalObjects[i].ID + " Name: " + myFinalObjects[i].Name); } mySession.Transaction.Commit(); }





:



StringBuilder messageString = new StringBuilder(); // for (int i = 0; i < 2; i++) { messageString.AppendLine("Comparing Class Object " + myInitialObjects[i].Name + " and DB Object " + myFinalObjects[i].Name + ". Result = " + myInitialObjects[i].Equals(myFinalObjects[i]).ToString()); } MessageBox.Show(messageString.ToString());





:



using (mySession.BeginTransaction()) { // Delete one object from Database mySession.Delete(myInitialObjects[0]); mySession.Transaction.Commit(); }





:



using (mySession.BeginTransaction()) { ICriteria criteria = mySession.CreateCriteria<employee>(); IList<employee> list = criteria.List<employee>(); StringBuilder messageString = new StringBuilder(); // Load and display the data foreach (Employee employee in list) { messageString.AppendLine("ID: " + employee.ID + " Name: " + employee.Name); } MessageBox.Show(messageString.ToString()); }





NHibernte . , .



Nhibernate.



Happy coding /////



: NHibernateBasics.zip



Nhiberante. Nhibernate ?
















.cs

.hbm.xml

.



.



<?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="NHibernateBasics" assembly="NHibernateBasics"> <class name="Employee" table="Employee"> <id name="ID" column="ID"> <generator class="identity"/> </id> <property name="Name" column="Name" /> </class> </hibernate-mapping>






XML Build Action = Embedded Resource.











(app.config).

.



<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" /> </configSections> <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2"> <session-factory> <property name="connection.provider"> NHibernate.Connection.DriverConnectionProvider</property> <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property> <property name="query.substitutions">hqlFunction=SQLFUNC</property> <property name="connection.driver_class"> NHibernate.Driver.SqlClientDriver</property> <property name="connection.connection_string"> Data Source=(Local)\SQL2008;Initial Catalog=NHibernateBasics; Integrated Security=True</property> <property name="show_sql">true</property> <mapping assembly="NHibernateBasics" /> </session-factory> </hibernate-configuration> </configuration>





Connection-string . CATALOG=, NHibernateBasics. mapping assembly=, NHibernateBasics.











. , .



:



using(mySession.BeginTransaction()) { // Insert two employees in Database mySession.Save(myInitialObjects[0]); mySession.Save(myInitialObjects[1]); mySession.Transaction.Commit(); }






:



using(mySession.BeginTransaction()) { // ICriteria criteria = mySession.CreateCriteria<employee>(); IList<employee> list = criteria.List<employee>(); for (int i = 0; i < myFinalObjects.Length; i++) { myFinalObjects[i] = list[i]; MessageBox.Show("ID: " + myFinalObjects[i].ID + " Name: " + myFinalObjects[i].Name); } mySession.Transaction.Commit(); }





:



StringBuilder messageString = new StringBuilder(); // for (int i = 0; i < 2; i++) { messageString.AppendLine("Comparing Class Object " + myInitialObjects[i].Name + " and DB Object " + myFinalObjects[i].Name + ". Result = " + myInitialObjects[i].Equals(myFinalObjects[i]).ToString()); } MessageBox.Show(messageString.ToString());





:



using (mySession.BeginTransaction()) { // Delete one object from Database mySession.Delete(myInitialObjects[0]); mySession.Transaction.Commit(); }





:



using (mySession.BeginTransaction()) { ICriteria criteria = mySession.CreateCriteria<employee>(); IList<employee> list = criteria.List<employee>(); StringBuilder messageString = new StringBuilder(); // Load and display the data foreach (Employee employee in list) { messageString.AppendLine("ID: " + employee.ID + " Name: " + employee.Name); } MessageBox.Show(messageString.ToString()); }





NHibernte . , .



Nhibernate.



Happy coding /////



: NHibernateBasics.zip



Nhiberante. Nhibernate ?



















All Articles