- Home
- Products
- Integration
- Tutorial
- Barcode FAQ
- Purchase
- Company
Employee.cs: A simple persistent class in C#
Listing 2.1 Employee.cs: A simple persistent class Encode Code 39 In Visual C# Using Barcode printer for Visual Studio .NET Control to generate, create Code 3/9 image in Visual Studio .NET applications. www.OnBarcode.comCode-39 Recognizer In C# Using Barcode reader for .NET framework Control to read, scan read, scan image in .NET applications. www.OnBarcode.comnamespace HelloNHibernate { class Employee { public int id; public string name; public Employee manager; public string SayHello() { return string.Format( "'Hello World!', said {0}.", name); } } } ANSI/AIM Code 39 Creator In Visual C#.NET Using Barcode creation for Visual Studio .NET Control to generate, create Code 39 Extended image in Visual Studio .NET applications. www.OnBarcode.comBarcode Generation In Visual C#.NET Using Barcode generator for Visual Studio .NET Control to generate, create Barcode image in Visual Studio .NET applications. www.OnBarcode.comThe Employee class has three fields: the identifier, the name of the employee, and a reference to the employee s manager. The identifier field allows the application to access the database identity the primary key value of a persistent object. If two instances of Employee have the same identifier value, they represent the same row in the database. We ve chosen int for the type of the identifier field, but this isn t a requirement. NHibernate allows virtually anything for the identifier type, as you ll see later. Note that you use public fields here rather than properties. This is purely to make the sample code shorter; it isn t always considered good practice. Instances of the Employee class may be managed (made persistent) by NHibernate, but they don t have to be. Because the Employee object doesn t implement any NHibernate-specific classes or interfaces, you can use it like any other .NET class: Data Matrix 2d Barcode Printer In C# Using Barcode printer for VS .NET Control to generate, create Data Matrix image in .NET applications. www.OnBarcode.comPrint 2D In C#.NET Using Barcode maker for .NET framework Control to generate, create 2D Barcode image in VS .NET applications. www.OnBarcode.com Hello World with NHibernate
UCC - 12 Generator In Visual C# Using Barcode creator for .NET framework Control to generate, create EAN / UCC - 14 image in .NET applications. www.OnBarcode.comMake Code-27 In Visual C# Using Barcode printer for .NET framework Control to generate, create ANSI/AIM Codabar image in VS .NET applications. www.OnBarcode.comEmployee fred = new Employee(); fred.name = "Fred Bloggs"; Console.WriteLine( fred.SayHello() ); Scanning ANSI/AIM Code 39 In Visual C#.NET Using Barcode decoder for VS .NET Control to read, scan read, scan image in VS .NET applications. www.OnBarcode.comCode 39 Full ASCII Printer In Objective-C Using Barcode generation for iPad Control to generate, create Code 39 image in iPad applications. www.OnBarcode.comThis code fragment does exactly what you ve come to expect from Hello World applications: it prints Hello World, said Fred Bloggs to the console. It may look like we re trying to be cute; in fact, we re demonstrating an important feature that distinguishes NHibernate from some other persistence solutions. The persistent class can be used with or without NHibernate no special requirements are needed. Of course, you came here to see NHibernate, so let s first set up the database and then demonstrate using NHibernate to save a new Employee to it. Making Code 39 Extended In .NET Framework Using Barcode encoder for Visual Studio .NET Control to generate, create Code 39 Extended image in Visual Studio .NET applications. www.OnBarcode.comUCC-128 Recognizer In C#.NET Using Barcode decoder for .NET Control to read, scan read, scan image in Visual Studio .NET applications. www.OnBarcode.comSetting up the database
Painting GS1 DataBar Limited In Java Using Barcode creator for Java Control to generate, create GS1 DataBar image in Java applications. www.OnBarcode.comCreate UCC-128 In VS .NET Using Barcode generator for VS .NET Control to generate, create EAN 128 image in Visual Studio .NET applications. www.OnBarcode.comYou need to have a database set up so that NHibernate has somewhere to save entities. Setting up a database for this program should only take a minute. NHibernate can work with many databases, but for this example you ll use Microsoft SQL Server 2000 or 2005. Your first step is to open Microsoft SQL Server Management Studio, connect to your database server, and open a new query window. Type the following in the SQL window to quickly create a new database: Scan Barcode In Visual C# Using Barcode Control SDK for VS .NET Control to generate, create, read, scan barcode image in VS .NET applications. www.OnBarcode.comCreate Code 3/9 In Objective-C Using Barcode creator for iPad Control to generate, create Code 3/9 image in iPad applications. www.OnBarcode.comCREATE DATABASE HelloNHibernate GO
QR Code ISO/IEC18004 Generation In Objective-C Using Barcode encoder for iPad Control to generate, create QR image in iPad applications. www.OnBarcode.comData Matrix ECC200 Generator In Visual Studio .NET Using Barcode encoder for VS .NET Control to generate, create ECC200 image in VS .NET applications. www.OnBarcode.comRun this SQL to create the database. The next step is to switch to that database and create a table to hold your Employee data. To do so, delete the previous SQL and replace it with the following Encoding Barcode In Visual Studio .NET Using Barcode maker for VS .NET Control to generate, create Barcode image in .NET applications. www.OnBarcode.comDrawing PDF417 In None Using Barcode creator for Office Excel Control to generate, create PDF-417 2d barcode image in Excel applications. www.OnBarcode.comUSE HelloNHibernate GO CREATE TABLE Employee ( id int identity primary key, name varchar(50), manager int ) GO Run this code: you ve created a place to store your Employee entities. You re now ready to see NHibernate in action! Note that, in chapter 9, we show you how to use NHibernate to automatically create the tables your application needs using just the information in the mapping files. There s some more SQL you won t need to write by hand! Creating an Employee and saving to the database
The code required to create an Employee and save it to the database is shown in listing 2.2. It comprises two functions: CreateEmployeeAndSaveToDatabase and OpenSession. You can type these functions into your Program.cs file below the static void Main() function in the Program class. Hello NHibernate! Listing 2.2 Creating and saving an Employee
static void CreateEmployeeAndSaveToDatabase() { Employee tobin = new Employee(); tobin.name = "Tobin Harris"; using (ISession session = OpenSession()) { using( ITransaction transaction = session.BeginTransaction() ) { session.Save(tobin); transaction.Commit(); } Console.WriteLine("Saved Tobin to the database"); } } static ISession OpenSession() { if(factory == null) { Configuration c = new Configuration(); c.AddAssembly(Assembly.GetCallingAssembly()); factory = c.BuildSessionFactory(); } return factory.OpenSession(); } static ISessionFactory factory; The CreateEmployeeAndSaveToDatabase function calls the NHibernate Session and Transaction interfaces. (We ll get to that OpenSession() call soon.) You re not ready to run the code just yet; but to give you an idea of what would happen, running the CreateEmployeeAndSaveToDatabase function would result in NHibernate executing some SQL behind the scenes: insert into Employees (name, manager) values ('Tobin Harris', null) Hold on the Id column isn t being initialized here. You didn t set the id field of message anywhere, so how can you expect it to get a value The id property is special: it s an identifier property it holds a unique value generated by the database. This generated value is assigned to the Employee instance by NHibernate during the call to the Save() method. We don t discuss the OpenSession function in depth here, but essentially it configures NHibernate and returns a session object that you can use to save, load, and search objects in your database (and much more!). Don t use this OpenSession function in your production projects; you ll learn more economical approaches throughout this book. You now have the Employee class defined, and have added an Employee table to your database. We ve also added some code to create an Employee instance and save it
|
|