Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.46 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. using System.Linq;
  5. using NHibernate;
  6. using NHibernate.Cfg;
  7.  
  8. namespace NHibernatePets {
  9.     public class Pet {
  10.         virtual public int id { get; set;}
  11.         virtual public string PetName { get; set;}
  12.         virtual public string Species { get; set; }
  13.         virtual public DateTime Birthday { get; set; }
  14.  
  15.         virtual public string Speak() {
  16.             return "Hi.  My name is '" + PetName + "' and I'm a " + Species + " born on " + Birthday + ".";
  17.         }
  18.     }
  19.  
  20.     public class Program {
  21.         private static void Main() {
  22.             Pet rosey = new Pet { PetName = "Rosey", Species = "Cat", Birthday = new DateTime(2009, 1, 1, 10, 5, 15) };
  23.             Console.WriteLine(rosey.Speak());
  24.  
  25.             //let's save rosey to the database
  26.             try {
  27.                 using (ISession session = OpenSession()) {
  28.                     using (ITransaction transaction = session.BeginTransaction()) {
  29.                         session.Save(rosey);
  30.                         transaction.Commit();
  31.                     }
  32.                     Console.WriteLine("Saved rosey to the database");
  33.                 }
  34.             } catch (Exception e) { Console.WriteLine(e); }
  35.             //let's read all the pets in the database
  36.             using (ISession session = OpenSession())
  37.             {
  38.                 IQuery query = session.CreateQuery("FROM Pet");
  39.                 IList<Pet> pets = query.List<Pet>();
  40.                 Console.Out.WriteLine("pets.Count = " + pets.Count);
  41.                 pets.ToList().ForEach(p => Console.WriteLine(p.Speak()));
  42.             }
  43.             //let's update our pet in the database
  44.             using (ISession session = OpenSession()) {
  45.                 using (ITransaction transaction = session.BeginTransaction())
  46.                 {
  47.                     IQuery query = session.CreateQuery("FROM Pet WHERE PetName = 'Rosey'");
  48.                     Pet pet = query.List<Pet>()[0];
  49.                     pet.PetName = "Rosie";
  50.                     transaction.Commit();
  51.                 }
  52.  
  53.             }
  54.             //let's read all the pets in the database (again)
  55.             using (ISession session = OpenSession()) {
  56.                 IQuery query = session.CreateQuery("FROM Pet");
  57.                 IList<Pet> pets = query.List<Pet>();
  58.                 Console.Out.WriteLine("pets.Count = " + pets.Count);
  59.                 pets.ToList().ForEach(p => Console.WriteLine(p.Speak()));
  60.             }
  61.             //let's delete our pet from the database
  62.             using (ISession session = OpenSession()) {
  63.                 using (ITransaction transaction = session.BeginTransaction()) {
  64.                     IQuery query = session.CreateQuery("FROM Pet WHERE PetName = 'Rosie'");
  65.                     Pet pet = query.List<Pet>()[0];
  66.                     session.Delete(pet);
  67.                     transaction.Commit();
  68.                 }
  69.  
  70.             }
  71.         }
  72.         static ISessionFactory SessionFactory;
  73.         static ISession OpenSession() {
  74.             if (SessionFactory == null) //not threadsafe
  75.                 { //SessionFactories are expensive, create only once
  76.                 Configuration configuration = new Configuration();
  77.                 configuration.AddAssembly(Assembly.GetCallingAssembly());
  78.                 SessionFactory = configuration.BuildSessionFactory();
  79.             }
  80.             return SessionFactory.OpenSession();
  81.         }
  82.     }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement