- Home
- Products
- Integration
- Tutorial
- Barcode FAQ
- Purchase
- Company
Hello World with NHibernate in C#
Hello World with NHibernate Draw Code 39 Extended In C# Using Barcode creation for .NET Control to generate, create ANSI/AIM Code 39 image in .NET framework applications. www.OnBarcode.comDecoding ANSI/AIM Code 39 In C#.NET Using Barcode reader for .NET framework Control to read, scan read, scan image in .NET applications. www.OnBarcode.comto the database using NHibernate. To complete the program, you will next add some code that will load the Employee from the database, and print our Hello World message. Let s add that code now. Linear 1D Barcode Printer In C#.NET Using Barcode creation for .NET framework Control to generate, create Linear 1D Barcode image in .NET applications. www.OnBarcode.comEAN13 Generator In Visual C# Using Barcode maker for VS .NET Control to generate, create EAN13 image in Visual Studio .NET applications. www.OnBarcode.comLoading an Employee from the database
Quick Response Code Creator In Visual C#.NET Using Barcode creation for .NET Control to generate, create Quick Response Code image in Visual Studio .NET applications. www.OnBarcode.comCode 39 Generator In C# Using Barcode drawer for .NET Control to generate, create Code 39 image in VS .NET applications. www.OnBarcode.comLet s start by adding code that can retrieve all Employees from the database in alphabetical order. Type the code in listing 2.3 below the previous OpenSession() function. Drawing Code 128C In Visual C#.NET Using Barcode generator for .NET Control to generate, create Code 128 Code Set B image in .NET framework applications. www.OnBarcode.com2 Of 5 Interleaved Printer In Visual C#.NET Using Barcode drawer for .NET Control to generate, create ANSI/AIM I-2/5 image in .NET applications. www.OnBarcode.comListing 2.3 Retrieving Employees
Code 39 Extended Generation In Java Using Barcode drawer for BIRT reports Control to generate, create Code-39 image in BIRT reports applications. www.OnBarcode.comCode-39 Creation In None Using Barcode maker for Font Control to generate, create Code39 image in Font applications. www.OnBarcode.comstatic void LoadEmployeesFromDatabase() { using (ISession session = OpenSession()) { IQuery query = session.CreateQuery( "from Employee as emp order by emp.name asc"); IList<Employee> foundEmployees = query.List<Employee>(); Console.WriteLine("\n{0} employees found:", foundEmployees.Count); foreach( Employee employee in foundEmployees ) Console.WriteLine(employee.SayHello()); } } Drawing PDF-417 2d Barcode In VS .NET Using Barcode drawer for .NET Control to generate, create PDF-417 2d barcode image in VS .NET applications. www.OnBarcode.comGenerating ANSI/AIM Code 39 In None Using Barcode maker for Microsoft Word Control to generate, create ANSI/AIM Code 39 image in Microsoft Word applications. www.OnBarcode.comThe literal string "from Employee as emp order by emp.name asc" is an NHibernate query, expressed in NHibernate s own object-oriented Hibernate Query Language (HQL). This query is internally translated into the following SQL when query.List() is called: Make DataMatrix In Objective-C Using Barcode printer for iPad Control to generate, create Data Matrix ECC200 image in iPad applications. www.OnBarcode.comEncode EAN128 In None Using Barcode creation for Software Control to generate, create EAN 128 image in Software applications. www.OnBarcode.comselect e.id, e.name, e.manager from Employee e order by e.name asc
Print Barcode In Java Using Barcode drawer for Java Control to generate, create Barcode image in Java applications. www.OnBarcode.comBarcode Encoder In Java Using Barcode creation for BIRT reports Control to generate, create Barcode image in Eclipse BIRT applications. www.OnBarcode.comIf you ve never used an ORM tool like NHibernate before, you were probably expecting to see the SQL statements somewhere in the code or metadata. They aren t there. All SQL is generated at runtime (at startup, where possible). So far, you ve defined the Employee entity, set up the database, and written code to create a new Employee and later retrieve it from the Employee table. NHibernate has barely entered the picture yet. Next, you ll write some XML to tell NHibernate about the Employee entity and how you want Employees saved in the database. Decode USS Code 128 In Visual C# Using Barcode reader for VS .NET Control to read, scan read, scan image in Visual Studio .NET applications. www.OnBarcode.comEncode Barcode In .NET Using Barcode generation for .NET Control to generate, create Barcode image in VS .NET applications. www.OnBarcode.comCreating a mapping file
Barcode Generation In None Using Barcode encoder for Office Excel Control to generate, create Barcode image in Microsoft Excel applications. www.OnBarcode.comData Matrix ECC200 Encoder In .NET Using Barcode creator for ASP.NET Control to generate, create Data Matrix ECC200 image in ASP.NET applications. www.OnBarcode.comIn order for NHibernate to do its magic in any of the code so far, it first needs more information about how the Employee class should be made persistent. This information is usually provided in an XML mapping document. The mapping document defines, among other things, how properties of the Employee class map to columns of the Employees table. Let s look at the mapping document in listing 2.4. Hello NHibernate! Listing 2.4 Simple Hibernate XML mapping
< xml version="1.0" > <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true"> <class name="HelloNHibernate.Employee, HelloNHibernate" lazy="false"> <id name="id" access="field"> <generator class="native" /> </id> <property name="name" access="field" column="name"/> <many-to-one access="field" name="manager" column="manager" cascade="all"/> </class> </hibernate-mapping> To add this mapping document to your solution, do the following: 2 3 4 Right-click your HelloNHibernate project in the Solution Explorer, and select Add > New Item. Select the XML document type, and name it Employee.hbm.xml. Click OK. Now highlight the XML file in Solution Explorer and look for the Build Action property in the Properties pane. Change it from Content to Embedded Resource. This is an important step that you shouldn t miss, because it allows NHibernate to easily find the mapping information. Now copy the XML in listing 2.4 into your Employee.hbm.xml file. The mapping document you ve just created tells NHibernate that the Employee class is to be persisted to the Employees table, that the id field maps to a column named id, that the name field maps to a column named name, and that the manager property is an association with many-to-one multiplicity that maps to a column named ManagerId. (Don t worry about the other details for now.) As you can see, the XML document isn t difficult to understand. You can easily write and maintain it by hand. In chapter 3, we discuss a way to generate the XML file from comments embedded in the source code. Whichever method you choose, NHibernate has enough information to completely generate all the SQL statements needed to insert, update, delete, and retrieve instances of the Employee class. You no longer need to write these SQL statements by hand. NOTE
NHibernate has sensible defaults that minimize typing and a mature document type definition that can be used for auto-completion or validation in editors, including Visual Studio. You can even automatically generate metadata with various tools.
|
|