O'Reilly logo

NHibernate 3 Beginner's Guide by Gabriel N. Schenker

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Persisting objects to the database

The whole ceremony of defining mappings, creating a database schema, creating a session factory, and finally creating and opening a session object has one single purpose: we want to persist the data created by our product inventory application to a durable data store.

Let's first create a new category object. We can do this by using the following code snippet:

var category = new Category 
  {
   Name = "Beverages", 
   Description = "Some description"
  };

Next, we want to save this new category object and we can do so by using the following code:

var id = session.Save(category);

The value that is returned from the save method corresponds to the ID of the newly created category object.

Now, let's create a product. The product ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required