Your first NHibernate based application

In a previous article I showed how to setup a developer machine to start using NHibernate as an ORM tool during the development of an application. I advocated a domain driven design (DDD) approach and a test driven development (TDD) style. This is the second article in a series of introductory chapters.

Define the Domain

Lets start by defining a very simple domain. For the moment it consists of one entity called Product. The product has 3 properties Name, Category and Discontinued.

image

Add a folder Domain to the FirstSample project of your solution. Add a new class Product.cs to this folder. The code is very simple and uses automatic properties (a feature of the new C# 3.0 compiler)

namespace FirstSolution.Domain
{
    public class Product
    {
        public string Name { get; set; }
        public string Category { get; set; }
        public bool Discontinued { get; set; }
    }
}

Now we want to be able to persist instances of this entity in a (relational) database. We have chosen NHibernate for this task. An instance of an entity in the domain corresponds to a row in a table in the database. So we have to define a mapping between the entity and the corresponding table in the database. This mapping can be done either by defining a mapping file (an xml-document) or by decorating the entity with attributes. I'll start with the mapping file.

Define the Mapping

Create a folder Mappings in the FirstSample project. Add a new xml-document to this folder and call it Person.hbm.xml. Please note the "hbm" part of the file name. This is a convention used by NHibernate to automatically recognize the file as a mapping file. Define "Embedded Resource" as Build Action for this xml file.

In the Windows Explorer locate the nhibernate-mapping.xsd in the src folder of NHibernate and copy it to your SharedLibs folder. We can now use this xml schema definition file when defining our mapping files. VS will then provide intellisense and validation when editing an xml mapping document.

Back in VS add the schema to the Product.hbm.xml file

image

Let's start now. Each mapping file has to define a <hibernate-mapping> root node

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" 
                   assembly="FirstSolution" 
                   namespace="FirstSolution.Domain">
 
  <!-- more mapping info here -->
  
</hibernate-mapping>

In a mapping file when referencing a domain class you always have to provide the fully qualified name of the class (e.g. FirstSample.Domain.Product, FirstSample). To make the xml less verbose you can define the assembly name (in which the domain classes are implemented and the namespace of the domain classes in the two attributes assembly and namespace of the root node. It's similar to the using statement in C#.

Now we have to first define a primary key for the product entity. Technically we could take the property Name of the product since this property must be defined and has to be unique. But it is common to use a surrogate key instead. For thus we add a property to our entity and call it Id. We use Guid as the type of the Id but it can as well be an int or a long.

using System;
 
namespace FirstSolution.Domain
{
    public class Product
    {
        public Guid Id { get; set; }
        public string Name { get; set; }
        public string Category { get; set; }
        public bool Discontinued { get; set; }
    }
}

The complete mapping file

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" 
                   assembly="FirstSolution" 
                   namespace="FirstSolution.Domain">
  
  <class name="Product">
    <id name="Id">
      <generator class="guid" />
    </id>
    <property name="Name" />
    <property name="Category" />
    <property name="Discontinued" />
  </class>
  
</hibernate-mapping>

NHibernate doesn't get in our way such as that it defines many reasonable defaults. So if you don't provide a column name for a property explicitly it will name the column according to the property. Or NHibernate can automatically infer the name of the table or the type of the column from the class definition. As a consequence my xml mapping file is not cluttered with redundant information. Please refer to the online documentation for more detailed explanation of the mapping. You can find it here.

Your solution explorer should look like this now (Domain.cd contains the class diagram of our simple domain)

image

Configure NHibernate

We now have to tell NHibernate which database product we want to use and provide it the connection details in form of a connection string. NHibernate supports many many database products!

Add a new xml file to the FirstSolution project and call it hibernate.cfg.xml. Set its property "Copy to Output" to "Copy always". Since we are using SQL Server Compact Edition in this first sample enter the following information into the xml file

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
  <session-factory>
    <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
    <property name="dialect">NHibernate.Dialect.MsSqlCeDialect</property>
    <property name="connection.driver_class">NHibernate.Driver.SqlServerCeDriver</property>
    <property name="connection.connection_string">Data Source=FirstSample.sdf</property>
    
    <property name="show_sql">true</property>
  </session-factory>
</hibernate-configuration>

With this configuration file we tell NHibernate that we want to use MS SQL Server Compact Edition as our target database and that the name of the database shall be FirstSample.sdf (=connection string). We have also defined that we want to see the SQL NHibernate generates and sends to the datbase (highly recommended for debugging purposes during development). Double check that you have no typos in the code!

Add an empty database called FirstSample.sdf to the FirstSample project (choose Local Database as template)

image

Click Add and ignore the DataSet creation wizard (just hit Cancel).

Test the Setup

It's now time to test our setup. First verify that you have the following files in your SharedLibs folder

image

The last 8 files you can find in the "Microsoft SQL Server Compact Edition" directory in your Programs folder.

image

Note: the System.Data.SqlServerCe.dll is located in the sub-folder Desktop.

All other files can be found in the NHibernate folder

Add a reference to the FirstSample project in your test project. Additionally add references to NHibernate.dll, nunit.framework.dll and Systm.Data.SqlServerCe.dll (remember to reference the files located in the SharedLibs folder!). Pay attention to set the property "Copy Local" to true for the assembly System.Data.SqlServerCe.dll since by default it is set to false!

Add a class called GenerateSchema_Fixture to your test project. Your test project should now look like this

image

We further need the 7 files sqce*.dll in the output directory. We can do this by using a post-build event in VS. Enter the following command in the "Post-build event command line"

copy $(ProjectDir)..\..\SharedLibs\sqlce*.dll $(ProjectDir)$(OutDir)

image

Now add the following code to the GenerateSchema_Fixture file

using FirstSolution.Domain;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;
using NUnit.Framework;
 
namespace FirstSolution.Tests
{
    [TestFixture]
    public class GenerateSchema_Fixture
    {
        [Test]
        public void Can_generate_schema()
        {
            var cfg = new Configuration();
            cfg.Configure();
            cfg.AddAssembly(typeof (Product).Assembly);
            
            new SchemaExport(cfg).Execute(false, true, false, false);
        }
    }
}

The first line of the test method creates a new instance of the NHibernate configuration class. This class is used to configure NHibernate. In the second line we tell NHibernate to configure itself. NHibernate will look out for configuration information since we do not provide any information here in the test method. So NHibernate will search for a file called hibernate.cfg.xml in the output directory. That's exactly what we want since we have defined our settings in this file.

In the third line of the code we tell NHibernate that it can find mapping information in the assembly which contains also the class Product. At the time being it will only find one such file (Product.hbm.xml) as an embedded resource.

The fourth line of code uses the SchemaExport helper class of NHibernate to auto-"magically" generate the schema in the database for us.

Note: with this test method we do NOT want to find out whether NHibernate does its job correctly (you can be sure it does) but rater whether we have setup our system correctly.

If you have TestDriven.Net installed you can now just right click inside the test method and choose "Run Test(s)" to execute the test.

image

If every thing is ok you should see the following result in the output window

image

If you have ReSharper installed you can just start the test by clicking the yellow-green circle on the left border and choose Run.

image

The result is as follows

image

In case of Problems

If your test fails double check that you find the following files in your target directory (that is: m:dev\projects\FirstSolution\src\FirstSolution.Tests\bin\debug)

image

Double check also if you have no typos in the NHibernate configuration file (hibernate.cfg.xml) or in the mapping file (Product.hbm.xml). Finally check whether you have set the "Build Action" of the mapping file (Product.hbm.xml) to "Embedded Resource". Only continue if the test succeeds.

Our first CRUD operations

Now obviously our system is ready to start. We have successfully implemented our Domain, defined the mapping files and configured NHibernate. Finally we have used NHibernate to automatically generate the database schema from our Domain (and our mapping files).

In the spirit of DDD (see e.g. Domain Driven Design by Eric Evans) we define a repository for all crud operations (create, read, update and delete). The repository interface is part of the domain where as the implementation is not! The implementation is infrastructure specific. We want to keep our domain persistence ignorant (PI).

Add a new interface to the domain folder of our FirstSolution project. Call it IProductRepository. Let's define the following interface

using System;
using System.Collections.Generic;
 
namespace FirstSolution.Domain
{
    public interface IProductRepository
    {
        void Add(Product product);
        void Update(Product product);
        void Remove(Product product);
        Product GetById(Guid productId);
        Product GetByName(string name);
        ICollection<Product> GetByCategory(string category);
    }
}

Add a class ProductRepository_Fixture to the test project of the solution and add the following code

    [TestFixture]
    public class ProductRepository_Fixture
    {
        private ISessionFactory _sessionFactory;
        private Configuration _configuration;
 
        [TestFixtureSetUp]
        public void TestFixtureSetUp()
        {
            _configuration = new Configuration();
            _configuration.Configure();
            _configuration.AddAssembly(typeof (Product).Assembly);
            _sessionFactory = _configuration.BuildSessionFactory();
        }
    }

 

In the fourth line of the method TestFixtureSetUp we create a session factory. This is an expensive process and should thus be executed only once. That's the reason why I put it into this method which is only executed once during a test cicle.

To keep our test methods side effect free we re-create our database schema before the execution of each test method. Thus we add the following method

        [SetUp]
        public void SetupContext()
        {
            new SchemaExport(_configuration).Execute(false, true, false, false);
        }

 

And now we can implement the test method to add a new product instance to the database. Start by adding a new folder called Repositories to your FirstSolution project. Add a class ProductRepository to this folder. Make the ProductRepository inherit from the interface IProductRepository.
using System;
using System.Collections.Generic;
using FirstSolution.Domain;
 
namespace FirstSolution.Repositories
{
    public class ProductRepository : IProductRepository
    {
        public void Add(Product product)
        {
            throw new NotImplementedException();
        }
 
        public void Update(Product product)
        {
            throw new NotImplementedException();
        }
 
        public void Remove(Product product)
        {
            throw new NotImplementedException();
        }
 
        public Product GetById(Guid productId)
        {
            throw new NotImplementedException();
        }
 
        public Product GetByName(string name)
        {
            throw new NotImplementedException();
        }
 
        public ICollection<Product> GetByCategory(string category)
        {
            throw new NotImplementedException();
        }
    }
}

Manipulating Data

Now go back to the ProductRepository_Fixture test class and implement the first test method

        [Test]
        public void Can_add_new_product()
        {
            var product = new Product {Name = "Apple", Category = "Fruits"};
            IProductRepository repository = new ProductRepository();
            repository.Add(product);
        }

The first run of the test method will fail since we have not yet implemented the Add method in the repository class. Let's do it. But wait, we have to define a little helper class first which provides us session objects on demand.

using FirstSolution.Domain;
using NHibernate;
using NHibernate.Cfg;
 
namespace FirstSolution.Repositories
{
    public class NHibernateHelper
    {
        private static ISessionFactory _sessionFactory;
 
        private static ISessionFactory SessionFactory
        {
            get
            {
                if(_sessionFactory == null)
                {
                    var configuration = new Configuration();
                    configuration.Configure();
                    configuration.AddAssembly(typeof(Product).Assembly);
                    _sessionFactory = configuration.BuildSessionFactory();
                }
                return _sessionFactory;
            }
        }
 
        public static ISession OpenSession()
        {
            return SessionFactory.OpenSession();
        }
    }
}

This class creates a session factory only the first time a client needs a new session.

Now we can define the Add method in the ProductRepository as follows

        public void Add(Product product)
        {
            using (ISession session = NHibernateHelper.OpenSession())
                using (ITransaction transaction = session.BeginTransaction())
                {
                    session.Save(product);
                    transaction.Commit();
                }
        }

The second run of the test method will again fail with the following message

image

That's because NHibernate is by default configured to use lazy load for all entities. That is the recommended approach and I warmly recommend not to change it for a maximum of flexibility.

How can we solve this issue? It's easy we have to just make all our properties (and methods) of the domain object(s) virtual. Let's do this for our Product class

    public class Product
    {
        public virtual Guid Id { get; set; }
        public virtual string Name { get; set; }
        public virtual string Category { get; set; }
        public virtual bool Discontinued { get; set; }
    }

Now run the test again. It should succeed and we get the following output

image

Note the sql spit out by NHibernate.

Now we think that we have successfully inserted a new product into the database. But let's test it whether it is really so. Let's extend our test method

        [Test]
        public void Can_add_new_product()
        {
            var product = new Product {Name = "Apple", Category = "Fruits"};
            IProductRepository repository = new ProductRepository();
            repository.Add(product);
 
            // use session to try to load the product
            using(ISession session = _sessionFactory.OpenSession())
            {
                var fromDb = session.Get<Product>(product.Id);
                // Test that the product was successfully inserted
                Assert.IsNotNull(fromDb);
                Assert.AreNotSame(product, fromDb);
                Assert.AreEqual(product.Name, fromDb.Name);
                Assert.AreEqual(product.Category, fromDb.Category);
            }
        }

Run the test again. Hopefully it will succeed...

Now we are ready to implement also the other methods of the repository. For testing this we would rather have a repository (that is database table) already containing some products. Nothing easier than this. Just add a method CreateInitialData to the test class as follows

        private readonly Product[] _products = new[]
                 {
                     new Product {Name = "Melon", Category = "Fruits"},
                     new Product {Name = "Pear", Category = "Fruits"},
                     new Product {Name = "Milk", Category = "Beverages"},
                     new Product {Name = "Coca Cola", Category = "Beverages"},
                     new Product {Name = "Pepsi Cola", Category = "Beverages"},
                 };
 
        private void CreateInitialData()
        {
            
            using(ISession session = _sessionFactory.OpenSession())
                using(ITransaction transaction = session.BeginTransaction())
                {
                    foreach (var product in _products)
                        session.Save(product);
                    transaction.Commit();
                }
        }

Call this method from the SetupContext method (after the create schema call) and we are done. Now each time after the database schema is created the database is populated with some products.

Let's test the Update method of the repository with the following code

        [Test]
        public void Can_update_existing_product()
        {
            var product = _products[0];
            product.Name = "Yellow Pear";
            IProductRepository repository = new ProductRepository();
            repository.Update(product);
 
            // use session to try to load the product
            using (ISession session = _sessionFactory.OpenSession())
            {
                var fromDb = session.Get<Product>(product.Id);
                Assert.AreEqual(product.Name, fromDb.Name);
            }
        }

When running for the first time this code will fail since the Update method has not yet been implemented in the repository. Note: This is the expected behavior since in TDD the first time you run a test it should always fail!

Analogous to the Add method we implement the Update method of the repository. The only difference is that we call the update method of the NHibernate session object instead of the save method.

        public void Update(Product product)
        {
            using (ISession session = NHibernateHelper.OpenSession())
            using (ITransaction transaction = session.BeginTransaction())
            {
                session.Update(product);
                transaction.Commit();
            }
        }

Run the test again an watch it succeed.

image

The delete method is straight forward. When testing whether the record has really been deleted we just assert that the value returned by the session's get method is equal to null. Here is the test method

        [Test]
        public void Can_remove_existing_product()
        {
            var product = _products[0];
            IProductRepository repository = new ProductRepository();
            repository.Remove(product);
 
            using (ISession session = _sessionFactory.OpenSession())
            {
                var fromDb = session.Get<Product>(product.Id);
                Assert.IsNull(fromDb);
            }
        }

and here the implementation of the Remove method in the repository

        public void Remove(Product product)
        {
            using (ISession session = NHibernateHelper.OpenSession())
                using (ITransaction transaction = session.BeginTransaction())
                {
                    session.Delete(product);
                    transaction.Commit();
                }
        }

Querying the Database

We still have to implement the three methods which query the database for objects. Let's start with the most easy one, the GetById. First we write the test

        [Test]
        public void Can_get_existing_product_by_id()
        {
            IProductRepository repository = new ProductRepository();
            var fromDb = repository.GetById(_products[1].Id);
            Assert.IsNotNull(fromDb);
            Assert.AreNotSame(_products[1], fromDb);
            Assert.AreEqual(_products[1].Name, fromDb.Name);
        }

and then the code to fulfill the test

        public Product GetById(Guid productId)
        {
            using (ISession session = NHibernateHelper.OpenSession())
                return session.Get<Product>(productId);
        }

Now that was easy. For the following two methods we use a new method of the session object. Let's start with the GetByName method. As usual we write the test first

        [Test]
        public void Can_get_existing_product_by_name()
        {
            IProductRepository repository = new ProductRepository();
            var fromDb = repository.GetByName(_products[1].Name);
 
            Assert.IsNotNull(fromDb);
            Assert.AreNotSame(_products[1], fromDb);
            Assert.AreEqual(_products[1].Id, fromDb.Id);
        }

The implementation of the GetByName method can be done by using two different approaches. The first is using HQL (Hibernate Query Language) and the second one HCQ (Hibernate Criteria Query). Let's start with HQL. HQL is a object oriented query language similar (but not equal to) SQL.

        public Product GetByName(string name)
        {
            using (ISession session = NHibernateHelper.OpenSession())
            {
                Product product = session
                    .CreateQuery("from Product p where p.Name=:name")
                    .SetString("name", name)
                    .UniqueResult<Product>();
                return product;
            }
        }

In the above sample I have introduced a commonly used technique when using NHibernate. It's called fluent interfaces. As a result the code is less verbose and easier to understand. You can see that a HQL query is a string which can have embedded (named) parameters. Parameters are prefixed by a ':'. NHibernate defines many helper methods (like SetString used in the example) to assign values of various types to those parameters. Finally by using UniqueResult I tell NHibernate that I expect only one record to return. If more than one record is returned by the HQL query then an exception is raised. To get more information about HQL please read the online documentation.

The second version uses a criteria query to search the requested product.

        public Product GetByName(string name)
        {
            using (ISession session = NHibernateHelper.OpenSession())
            {
                Product product = session
                    .CreateCriteria(typeof(Product))
                    .Add(Restrictions.Eq("Name", name))
                    .UniqueResult<Product>();
                return product;
            }
        }

Many users of NHibernate think that this approach is more object oriented. On the other hand a complex query written with criteria syntax can quickly become difficult to understand.

The last method to implement is GetByCategory. This method returns a list of products. The test can be implemented as follows

        [Test]
        public void Can_get_existing_products_by_category()
        {
            IProductRepository repository = new ProductRepository();
            var fromDb = repository.GetByCategory("Fruits");
 
            Assert.AreEqual(2, fromDb.Count);
            Assert.IsTrue(IsInCollection(_products[0], fromDb));
            Assert.IsTrue(IsInCollection(_products[1], fromDb));
        }
 
        private bool IsInCollection(Product product, ICollection<Product> fromDb)
        {
            foreach (var item in fromDb)
                if (product.Id == item.Id)
                    return true;
            return false;
        }

and the method itself might contain the following code

        public ICollection<Product> GetByCategory(string category)
        {
            using (ISession session = NHibernateHelper.OpenSession())
            {
                var products = session
                    .CreateCriteria(typeof(Product))
                    .Add(Restrictions.Eq("Category", category))
                    .List<Product>();
                return products;
            }
        }

Summary

In this article I have shown you how to implement a basic sample domain, define the mapping to a database and how to configure NHibernate to be able to persist domain objects in the database. I have shown you how to typically write and test CRUD methods for your domain objects. I have taken MS SQL Compact Edition as sample database but any other supported database can be used (you only have to change the hibernate.cfg.xml file accordingly). Ee have no dependencies on external frameworks or tools other than the database and NHibernate itself (.NET of course never counts here).

Print | posted on Tuesday, April 01, 2008 12:29 AM

Comments on this post

# re: Your first NHibernate based application

Requesting Gravatar...
Who is the author of this piece? Very comprehensive, well done.
Left by Matt on Apr 01, 2008 1:51 PM

# re: Your first NHibernate based application

Requesting Gravatar...
Matt: Thanks for your nice comment. I'm the author of this article
Left by Gabriel Schenker on Apr 01, 2008 5:27 PM

# re: Your first NHibernate based application

Requesting Gravatar...
well founded, very good explained, helpful examples, a veritable treasure trove, in which one can learn so much. Keep up that good work!
Left by Peter on Apr 02, 2008 7:44 PM

# re: Your first NHibernate based application

Requesting Gravatar...
great introduction to NHibernate, it helped me a lot (Im a NH newbie :)
Left by Libor on Apr 04, 2008 3:49 AM

# re: Your first NHibernate based application

Requesting Gravatar...
Very good work, Gabriel!

But as a domain should be PI I would suggest that you define the mappingfiles in its own external library. I have started using NHibernate for the first time, and it was no a hard job to get it to work. It takes only one second to define the mappinghandling in the cfg.xml file.

Benny
Left by Benny on Apr 04, 2008 8:13 PM

# re: Your first NHibernate based application

Requesting Gravatar...
@Benny: you are correct. For a proper separation of concerns (SoC) one can put the mapping files (as well as the implementation of the repositories) in a separate library (Project). But I wanted to keep the complexity of the sample as low as possible. And there has also been a lively discussion in the ALT.NET group about having many or as few as possible projects per solution...
Left by Gabriel Schenker on Apr 05, 2008 2:05 AM

# re: Your first NHibernate based application

Requesting Gravatar...
Gabriel, thanks for this article. It's a great introduction to NHibernate that's really helped me see and I've really enjoyed working through it. Superb all round!

Thanks, Sean
Left by Sean Kearon on Apr 05, 2008 4:46 AM

# re: Your first NHibernate based application

Requesting Gravatar...
@Gabriel:I only heard the correct, that was enough for me :). I agree that the complexity gets lowered by having fewer projects. But it's kind of misleading when you say that you want to do thing in the heart of DDD and not doing that crusial thing about DDD. But your post was so good that I'll let that past.

Keep up the good work.
Left by Benny on Apr 05, 2008 7:45 AM

# re: Your first NHibernate based application

Requesting Gravatar...
@Sean: It's a pleasure for me to help other people! I also profited a lot from other blogs.

@Benny: rest assured that in future posts where I'll write on advanced topics I'll be stricter in following the patterns and best practices.
Left by Gabriel Schenker on Apr 05, 2008 8:31 AM

# re: Your first NHibernate based application

Requesting Gravatar...
Gabriel, you are awesome. Thanks for making this so crystal clear! Please, please, please keep posting more on TDD, NHibernate, DDD, and more...
Left by Peter on May 12, 2008 3:18 PM

# re: Your first NHibernate based application

Requesting Gravatar...
This is a great start to using NHibernate and to familiarizing ourselves with the latest way of development. This is about my first touch with NHibernate technology, but really not the last one!

Danke schön Gabriel!
Left by Will on Jun 02, 2008 7:08 AM

# re: Your first NHibernate based application

Requesting Gravatar...
Fantastic hands-on introduction to nHibernate. A good article to get started, keep up the good work.
Left by Kannan on Jul 01, 2008 1:40 AM

# re: Your first NHibernate based application

Requesting Gravatar...
hey this is some great stuff!
Left by james on Jul 04, 2008 12:09 AM

# re: Your first NHibernate based application

Requesting Gravatar...
Great article. Can you continue to write a followup article explaining how to persist when inheritance comes into play?
Thanks in advance!
Left by tobsen on Jul 04, 2008 10:45 PM

# re: Your first NHibernate based application

Requesting Gravatar...
Good job. Exciting article. I followed along... except that I used VS.NET 2005, VB.NET, and mySQL.
Left by Kris Krause on Jul 11, 2008 9:18 AM

# re: Your first NHibernate based application

Requesting Gravatar...
I found that beside setting "Embedded Resource" as "Build Action" for the mapping and configuration file I also had to set the property "Copy to output directory" to "Copy always"
Left by Ronald on Jul 18, 2008 12:37 AM

# re: Your first NHibernate based application

Requesting Gravatar...
@Ronald: you ONLY have to set "Copy to output directory" to "Copy always" for the configuration file but NOT for the mapping files since the latter will be embedded in the binary and thus NOT needed as stand-alone files in the bin directory!
Left by Gabriel Schenker on Jul 20, 2008 6:12 PM

# re: Your first NHibernate based application

Requesting Gravatar...
Very comprehensive, great illustrations, simple but practical...and light on the philosophical insight of coding practices.

Thanks for putting this together.

Well done!
Left by BB on Jul 21, 2008 5:50 PM

# re: Your first NHibernate based application

Requesting Gravatar...
Can you explain why a transaction is always required when calling Session.Save(entity)?

Great series of articles. Thanks! :)
Left by Eric on Jul 23, 2008 5:37 AM

# re: Your first NHibernate based application

Requesting Gravatar...
Very good article. But I have a questions. I don't want to use xml file. Is it possible? Annotations?
Left by Honovan on Jul 25, 2008 1:04 AM

# re: Your first NHibernate based application

Requesting Gravatar...
@Honovan: yes there are at least two
a) use Castle Active Record (annotate the entities with Attributes) or
b) use an internal DSL as described by Jeremy D. Miller in his blog (http://codebetter.com/blogs/jeremy.miller/archive/2008/06/18/working-faster-and-fewer-mapping-errors-with-nhibernate.aspx)
Left by Gabriel Schenker on Jul 27, 2008 3:47 AM

# re: Your first NHibernate based application

Requesting Gravatar...
@Eric: a transaction is needed because NHibernate only caches any changes internally when calling save and does NOT "auto-magically" save it to the database. Only when committing a transaction are all pending changes flushed to the database. (For further details see also the articles about the Unit of Work pattern in this blog)
Left by Gabriel Schenker on Jul 27, 2008 3:51 AM

# re: Your first NHibernate based application

Requesting Gravatar...
Hi!
Do you have to hand craft the mapping file? Is there anyway to generate this. ???
Left by fuzze on Jul 28, 2008 11:56 AM

# re: Your first NHibernate based application

Requesting Gravatar...
@fuzze: yes I always prefer to hand-craft the mappings. I don't like generated code too much. But there are tools to automatically generate the hbm files. Easiest solution (if you don't mind to "pollute" your domain entities with mapping attributes) is certainly "Castle Active Record" or "NHibernate Attributes"
Left by Gabriel Schenker on Jul 28, 2008 5:33 PM

# re: Your first NHibernate based application

Requesting Gravatar...
Thanx for the reply.. any links at hand for me to check out? I prefer keeping my entities simple although I'm timestrapped at the moment:-)
Left by fuzze on Jul 28, 2008 7:45 PM

# re: Your first NHibernate based application

Requesting Gravatar...
Thanks for the reply. I´m java programmer, and I'm starting in .net framework.
Left by Honovan on Jul 29, 2008 1:19 AM

# re: Your first NHibernate based application

Requesting Gravatar...
Hello,

sorry for bugging in, but, could you please provide me the code for the project you worked here on? i am a new and young C#+NHibernate developer, and it would greately help me. of course if, you still have it...

thanks.
Left by Bogdan on Aug 02, 2008 7:12 AM

# re: Your first NHibernate based application

Requesting Gravatar...
Hello,

It's me again. I have managed quite well so far in learning NHibernate, but I have a slight problem I would like to ask you about.
I'll try to explain it here.

I have followed your example, step-by-step. Everything went well until I tried to populate my database with different products, and then test the Add method. So, the test for Add method went well for adding only one product, but when I call CreateInitialData(), I get this error:
=================================
TestCase 'WorkingOnNH_Test.ProductRepository_Fixture.AddTest'
failed: NHibernate.Exceptions.GenericADOException : could not insert: [WorkingOnNH.Domain.Product][SQL: INSERT INTO PRODUCTS (Name, Category, OnSale, ID) VALUES (?, ?, ?, ?)]
----> System.Data.OracleClient.OracleException : ORA-00001: unique constraint (BOGDAN.SYS_C004335) violated.
=================================
Indeed I use Oracle10g2XE, and not SQL, but I have managed to pass the FixtureSetupTest, and the single AddTest. I've started debugging, and I noticed the error is thrown at this line

======================
transaction.Commit();
======================

from CreateInitialData() method, that is identically with yours.

I am clueless, so please excuse me for posting this here. If can offer me another possibility to get in contact with you, I would apreciate it.
Left by Bogdan on Aug 04, 2008 1:46 AM

# re: Your first NHibernate based application

Requesting Gravatar...
@Bogdan: you can find the source code here:
http://hibernatingrhinos.googlecode.com/svn/trunk/FirstSolution/

Unfortunately the oracle community of NHibernate seems to be rather "inactive" and as a consequence there are some problems found here and there. I regret, I do not have an oracle db and thus cannot verify the behavior you mention. The cause of the exception might be the primary key of type Guid (Oracle does not support it). For possible solutions please check the NHUsers mail group (there have been some threads about this very topic)
Left by Gabriel Schenker on Aug 04, 2008 5:15 PM

# re: Your first NHibernate based application

Requesting Gravatar...
Gabriel Schenker: thank you so much for the blog and I'm a NH newbie the source code directory you provided it requires to downoad one file at a time, is there a way you can zip all the solution in file?
thanks again for great work and i hope to see more on NH

-Nisar
Left by Nisar Khan on Aug 05, 2008 5:48 AM

# re: Your first NHibernate based application

Requesting Gravatar...
thank you, it really helps
Left by Dian Pitaloka on Aug 05, 2008 10:48 PM

# re: Your first NHibernate based application

Requesting Gravatar...
@Nisar: you don't have to download it manually. Just install a SVN client tool like TortoiseSVN and point it to the URL. The tool will download the whole solution...
Left by Gabriel Schenker on Aug 07, 2008 5:03 PM

# re: Your first NHibernate based application

Requesting Gravatar...
This is really a great tutorial. Quite comprehensive and elaborate.
Left by Khalil Muhammad on Aug 13, 2008 10:09 PM

# re: Your first NHibernate based application

Requesting Gravatar...
This is the great article I have ever read on NHibernating. You brought every best tools TDD, NHibernating, NAnt..etc..together with marvelous related links. Pls keep going ... and like to learn more from you on other best practices on coding...but how can I ???
Left by Hasan on Aug 22, 2008 2:58 AM

# re: Your first NHibernate based application

Requesting Gravatar...
Great article, I am just trying to do my first app, and I am getting the following:

A project with an Output Type of Class Library cannot started directly.

The VS then recommends if I want to debug to add an executable project to this solution.

Is this what I have to do?, I expected the "FirstSolution.Test" could be debugged... what I am doing wrong?



Left by Olga on Aug 26, 2008 3:50 AM

# re: Your first NHibernate based application

Requesting Gravatar...
@Olga: A test project cannot be run like a console or winform application since it is a DLL. You need a Test-Runner application to do so. YOu have several possibilities. You have to either run the test project with the Unit-Test runner of Resharper (if you have installed it - highly recommended) or with Testdriven.net (there is a free and a commercial version).
Another possible solution is to use the test runner of NUnit (please read e.g. the documentation at www.nunit.org about how to do this)
Left by Gabriel Schenker on Aug 26, 2008 8:53 AM

# re: Your first NHibernate based application

Requesting Gravatar...
gr8, code.

It was actually my first NHibernate based application (walkthrough)
Left by pbarve on Aug 28, 2008 1:01 AM

# re: Your first NHibernate based application

Requesting Gravatar...
I've seen a couple of "introductions to NHibernate" but this is really great piece of work.

Beats all others by a mile :-).

And, if I may suggest something - .NET developers work with various editions of SQL Server (SQL Server Express probably being the most common), so I think it'd be helpful (especially to "newbies") for the article if you could somehow note the differences for various SQL Server database (I suppose all you need to change is connection string).

Once again, great work!
Left by Zvonimir Vanjak on Aug 28, 2008 1:41 AM

# Bugfixes for Post

Requesting Gravatar...
don't know whether there is a fix for this two "bugs":

1. there is a typo in the description for creating the Product.hbm.xml. (it's refered to as Person.hbm.xml)

2. the postbuild will only work in situations where no spaces are in the path. so brace your command with "" like:
copy "$(ProjectDir)..\..\SharedLibs\sqlce*.dll" "$(ProjectDir)$(OutDir)"
Left by Rainer Schuster on Sep 06, 2008 7:28 AM

# re: Your first NHibernate based application

Requesting Gravatar...
Great job, well done!

This is more than 'just' a introduction to NHibernate, it is also a an introduction to good software engineering tools.
Left by Günter Lissner on Sep 18, 2008 4:45 PM

# re: Your first NHibernate based application

Requesting Gravatar...
Nice article--I've got a couple of questions that I hope you can help me with:

1) The sqlce*.dll files don't copy (in VS 2008) without the post-build action, even if I set their build action to 'Content' and set 'Copy to Output Directory' property to 'Copy always'. Is there a simple explanation for why that is?

2) I include the post-build action in the main project, but when I compile, I get an error message saying the command "exited with Code 1". I can't find documentation on the error. Any idea what might by going on?

Thanks for your help.
Left by David Veeneman on Sep 26, 2008 11:13 AM

# re: Your first NHibernate based application

Requesting Gravatar...
I've got the post-build command working--the 'Code 1' error meant there was a syntax error in the command. My project file structure was slightly different than yours. Once I fixed that and added double quotes around the two copy parameters, it worked fine.

But I've still got my original question about using the post-build event: I know it doesn't work to simply set the "Copy to Output Directory" property for these files to "Copy Always", but I don't understand why. Any thoughts? Thanks for your help.
Left by David Veeneman on Sep 26, 2008 2:33 PM

# re: Your first NHibernate based application

Requesting Gravatar...
Cheers, just followed this to get me up-and-running very quickly.
Left by James on Sep 30, 2008 1:00 PM

# re: Your first NHibernate based application

Requesting Gravatar...
@David: I regret but I don't know why it doesn't work as you suggest. But never mind, we have a work-around with the post build event
Left by nhibernate on Oct 02, 2008 7:38 AM

# re: Your first NHibernate based application

Requesting Gravatar...
Easiest and most through article without doing a complete dump of information. Thank You!
Left by Junaid on Oct 05, 2008 3:58 PM

# re: Your first NHibernate based application

Requesting Gravatar...
Excellent article. You should write a book in this style. Very easy to follow and from nothing to a working CRUD framework in no time at all. (I don't see any NHibernate books out currently) Thanks!
Left by Jamie on Oct 13, 2008 11:24 AM

# re: Your first NHibernate based application

Requesting Gravatar...
Thank you for the great article. One thing I did not understand is how two entities of different type can be commited in one transaction. The CRUD-Operations of your entities all have their own transactions. Am I thinking too much in SQL?
Left by Aris on Oct 15, 2008 9:02 PM

# re: Your first NHibernate based application

Requesting Gravatar...
@Aris: you have to use the Unit of Work pattern. I have published a series about this pattern in this blog :-) or in the wiki of the nhforge site (www.nhforge.org)
Left by nhibernate on Oct 19, 2008 5:14 PM

# re: Your first NHibernate based application

Requesting Gravatar...
Absolutely great article.
Very informative and great to follow!

Good to see C#3 stuff in use too, makes it easier to follow.
Left by Daniel Hoelbling on Oct 19, 2008 9:44 PM

# re: Your first NHibernate based application

Requesting Gravatar...

This took me several hours to setup namely because I was trying to mesh a whole slew of hibernate resources from the web with this. This is actually a fantastic tutorial that stands on its own two feet. More depth could be added into the mapping file(s), but for what it does, it does quite well.

If you want to learn how to setup an NHibernate project and learn how to do Test Driven development with NUnit, this is the best place I've found to start. Just take a breath and be patient. You'll get there.
Left by jbaisden on Oct 21, 2008 4:48 AM

# re: Your first NHibernate based application

Requesting Gravatar...
Wow. Thanks for your help. I found your article on the UoW Pattern and implemented it. Now another beginners question: Can I use one UnitOfWork for a complete (Windows Forms) application, or is it better to keep the UoW small?
Left by Aris on Oct 24, 2008 4:05 AM

# re: Your first NHibernate based application

Requesting Gravatar...
I tried this method of configuration and was unable to successfully create the driver for CE. When I changed the line:
" NHibernate.Driver.SqlServerCeDriver"
to:

"NHibernate.Driver.SqlClientDriver"

it at least stopped throwing exceptions. Now, I haven't completed the lab yet, so perhaps in the long run it won't work, but that's what it took to at least get it to not throw.
Left by Brandon on Oct 24, 2008 4:50 AM

# re: Your first NHibernate based application

Requesting Gravatar...
@Aris: I would start an UoW when opening/refreshing a form and end the UoW when saving/leaving the form.
But be aware that in a WinForms (or WPF) application you can potentially have several concurrent UoW...
Left by nhibernate on Oct 27, 2008 6:12 PM

# re: Your first NHibernate based application

Requesting Gravatar...
Thanks again for your help. I read in another posting of yours that you are thinking about implementig a concurrency enabled UoW. I'm looking forward to see how you solve this problem. How could I support you?
Left by Aris on Nov 05, 2008 2:18 AM

# re: Your first NHibernate based application

Requesting Gravatar...
Good job. Very comprehensive and to the point.
Left by Deven on Nov 10, 2008 4:47 AM

# re: Your first NHibernate based application

Requesting Gravatar...
If you are using MS Test (rather than NUnit), add hibernate.cfg.xml to the list of deployed files in LocalTestRun.testrunconfig. Otherwise, MS Test will not be able to find it.
Left by David Veeneman on Nov 11, 2008 1:15 AM

# re: Your first NHibernate based application

Requesting Gravatar...
Amazing post to get started, easy to follow step by step.

I experienced a little problem at the first CRUD application. To get to the error on virtual properties, I had to configure the property proxyfactory.factory_class (NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle suggested by the exception thrown) and include the binary NHibernate.ByteCode.Castle.dll (or ...LinFu.dll).

Did I miss something? I'm using NHibernate 2.0.1.
Left by Eivind on Nov 11, 2008 5:35 AM

# re: Your first NHibernate based application

Requesting Gravatar...
@Eivind: Obviously You are using the trunk of NHibernate (version 2.1). There is a breaking change is so far that the hard coded dependency of NH to Castle DynamicProxy2 has been given up an replaced by an implementation that gives us the freedom to choose our proxy generator (at the time being LinFu or Castle).
My post has been written before this happened!
Left by nhibernate on Nov 15, 2008 11:21 PM

# re: Your first NHibernate based application

Requesting Gravatar...
Great! Thanks a lot!
Left by Joh on Nov 18, 2008 12:38 AM

# re: Your first NHibernate based application

Requesting Gravatar...
first i want to thank you for this post, its great.

im having a problem with the

copy $(ProjectDir)..\..\SharedLibs\sqlce*.dll $(ProjectDir)$(OutDir)

i get that the directory dont exist and this
copy C:\Users\Konnekt\Desktop\Domy\Domy.Test\..\..\SharedLibs\sqlce*.dll C:\Users\Konnekt\Desktop\Domy\Domy.Test\bin\Debug\" exited with code 1.

i have try to google but cant find a awnser to my problem. what can i do to fix it?
Left by Zoran on Nov 18, 2008 9:30 AM

# re: Your first NHibernate based application

Requesting Gravatar...
nevermind i just wanted it to work. i copy the files manually instead.
Left by Zoran on Nov 18, 2008 10:16 AM

# re: Your first NHibernate based application

Requesting Gravatar...
Thank you very much for this exelent Guide through the firsts steps of using NHibernate.

I'd like especially the test first approach.

Can you refere to some good tutorials using Spring with NHibernate? Thats my next quest to compete ;)


greetz

menty

Left by menty on Nov 19, 2008 6:40 AM

# re: Your first NHibernate based application

Requesting Gravatar...
Great tutorial but...

I also have a problem with the proxy and I don't understand how to fix it.

Could you please help me?
Which Libraries do I have to include und what di I have to write in the cfg.xml?

thanks
Telefisch
Left by Telefisch on Nov 20, 2008 12:11 AM

# re: Your first NHibernate based application

Requesting Gravatar...
something else...
I had the same Problems with the copy statement and fixed it with some "

So my copy-statement looks like this:
copy "$(ProjectDir)..\..\SharedLibs\sqlce*.dll" "$(ProjectDir)$(OutDir)"

perhaps its only a VS2008 problem...
Left by Telefisch on Nov 20, 2008 12:13 AM

# re: Your first NHibernate based application

Requesting Gravatar...
...proxy-problem fixed!
thanks.
Left by Telefisch on Nov 20, 2008 12:50 AM

# re: Your first NHibernate based application

Requesting Gravatar...
I have a few more question using UoWs in Windows Forms applications. Perhaps you can give me some advice.
To avoid concurrent UoWs, my first approach was to use a UoW for each user action. A great disadvantage of this approach is, that caching and lazy loading don't work anymore as soon as the UoW has ended.
So I began to think about the possibility of supporting concurrent UoWs, as you suggested before. The only solution that came into my mind was to save the Session/UoW as member variable in the form and passing it as argument to every repository object beeing used. I don't like this idea very much. Do you know of a better solution and could you give me a hint?

Do you know if sessions are garbage collected or do they have to be closed/disposed explicitly when closing the form?
Left by Aris on Nov 22, 2008 7:48 AM

# re: Your first NHibernate based application

Requesting Gravatar...
At a certain moment you say:

"The last 8 files you can find in the "Microsoft SQL Server Compact Edition" directory in your Programs folder."

But I don't want to use the compact edition. As far as i know i am using the "normal edition". What files should i reference to?
Left by Marco on Dec 02, 2008 3:40 AM

# re: Your first NHibernate based application

Requesting Gravatar...
Nice tutorial, much better than the others about, however i still have a few issues to iron out.

1. I am using Microsoft Sql Server 2005, can anyone point me in the direction to find out what .dll's need to go where to get that working ? I looked in the folder of MS Sql and found a good 20-30 dll files, cant need all of them surely.

2. Product class does NOT like that the get; set; parts are left blank. I added some variables to the class and filled the accessor methods with those but it seemed to be a large thing to leave out.

3. By default, VS 2005 seems to think that the output type of the projectS are "Class Library" it complains about this saying that outputs of that type cannot be started directly and i should link them to a project with a valid .exe. I changed the output type to "Windows Application" and then "Console Application", both of which reply with:

"FirstSolution\obj\Debug\FirstSolution.exe' does not contain a static 'Main' method suitable for an entry point

OR

FirstSolution\src\FirstSolution\bin\Debug\
FirstSolution.exe' could not be found

PLEASE, if anyone knows what is going on id be VERY appreciative as i have been trying to get nHibernate to work for weeks now with no sucess and the idea of ORM is very interesting to me.

Thank you for your time.
Left by David on Dec 02, 2008 4:30 AM

# re: Your first NHibernate based application

Requesting Gravatar...
@David: sorry for the long delay.
1) you don't have to reference any extra dll's when accessing any version of SQL Server (except the compact edition). The Sql Server driver is part of the "System.Data" dll which is part of the .NET framework.

2) I am using automatic properties. This is a feature of C# 3.0 which is available with VS 2008. So if you are still using VS 2005 you have to explicitely implement the properties (using e.g. a backing field)

3) Normally I don't present full featured applications in my post. The code I present is intended to be part of an application. The code as it is can only be run in a (unit-) test runner (e.g. the test runner of Resharper or NUnit)
Left by Gabriel N. Schenker on Dec 07, 2008 6:30 PM

# re: Your first NHibernate based application

Requesting Gravatar...
@Marco: if you use the SQL server standard edition you can just delete the "post build event command line" which I have defined in the post. You don't have to reference any extra dll since the sql server driver is part of the .NET framework (System.Data.dll).
Left by Gabriel N. Schenker on Dec 07, 2008 6:34 PM

# re: Your first NHibernate based application

Requesting Gravatar...
Ok, thank you for the info.

I have read alot about ORM and it seems to be a really good way of setting up a database against a website. Maybe thats just because me and SQL have never been the best of friends.

Gabriel N. Schenker - "Normally I don't present full featured applications in my post. The code I present is intended to be part of an application. The code as it is can only be run in a (unit-) test runner (e.g. the test runner of Resharper or NUnit)"

Ah ok, i have not looked into any unit testing software as of yet as i have not managed to get a project using nHibernate to even compile yet. Figured i will learn to crawl before i walk.

Thank you for your time on this matter and if any of you guys know of any resources that may help an abolsute beginner get of the ground in terms of unit testing, nHibernate i would be most grateful.

Once again, thank you.
Left by David on Dec 07, 2008 11:21 PM

# re: Your first NHibernate based application

Requesting Gravatar...
Nice Post! But I still have the same problem that Elvind...I already configured the property proxyfactory.factory_class but it didn't work, i have the following error:
"TestFixture failed: NHibernate.Bytecode.UnableToLoadProxyFactoryFactoryException : Unable to load type 'NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle' during configuration of proxy factory class."
Help!!
Left by Bren on Dec 18, 2008 2:12 PM

# Your first NHibernate based application

Requesting Gravatar...
@Bren: you are using the trunk. It is a new requirement of the trunk to add the following statement to the config file

<property name='proxyfactory.factory_class'>NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>

that is, you have to explicitly define which proxy factory you want to use.


Please also have a look at this post:
nhforge.org/.../nh2-1-0-bytecode-providers.aspx
Left by Gabriel N. Schenker on Dec 22, 2008 7:52 PM

# re: Your first NHibernate based application

Requesting Gravatar...
The Update test will not pass. I have checked and it appears that I have everything as the tutorial has it. The problem seems to be that I am trying to update a product with a guid of "00000-0000 ..." How can you stop Nunit from destroying the database to investigate?
Left by Nick on Dec 24, 2008 7:14 AM

# re: Your first NHibernate based application

Requesting Gravatar...
dooood this is that 3rd article ive been through and i got nada. a working example in the least possible lines of code is by far preferable to spending hours before being to compile and finding again that it dont work. It looks like a great product. pitty the doco is frankly sh1te
Left by ananon on Dec 24, 2008 5:13 PM

# re: Your first NHibernate based application

Requesting Gravatar...
@Nick: I do not fully understand your problem/question. A single unit test does NOT destroy the database. So just run the unit test that does not work and then you can examine the db.
Left by nhibernate on Jan 08, 2009 1:56 AM

# re: Your first NHibernate based application

Requesting Gravatar...
@aanon: can you be more precise about what you are talking, please...
Left by nhibernate on Jan 08, 2009 2:02 AM

# re: Your first NHibernate based application

Requesting Gravatar...
Thanks for this nice tutorial.

I don't quite understand how sessions and transactions are used in NHibernate. Each method in ProductRepository seems to create and use its own session+transaction. And then, the CreateInitialData seems to call the Repository.Save method in a loop within a new session+transaction context. How is the unit of work defined here?

Any help would be appreciated, thanks in advance.
Left by mg on Jan 16, 2009 8:43 AM

# re: Your first NHibernate based application

Requesting Gravatar...
@mg: in a real application one would probably use the Unit of Work pattern. The UoW would then be started by the application service layer.
See my posts about the UoW pattern in this blog.
Left by Gabriel N. Schenker on Jan 21, 2009 1:35 AM

# re: Your first NHibernate based application

Requesting Gravatar...
Nicely done,
Just the right amount of detail.
Very cleanly written article.
Thanks.
Left by Radu W. on Jan 30, 2009 6:06 AM

# re: Your first NHibernate based application

Requesting Gravatar...
Great works, Thanks.
It seems that session is not closed
Left by Hoare.WOo on Feb 01, 2009 1:19 AM

# re: Your first NHibernate based application

Requesting Gravatar...
Hi there - have built a test project in vb.net in vs 2008 using your examples and can't get past the following error.

The project is called MediaLoader. It does not seem to load the hbm.xml file with the mapping. The namespace is definetaly correct, and I am at a loss. Your help would be appreciated

Steve

TestCase 'Dev.MediaRepository_Fixture.can_add_new_product'
failed: System.TypeLoadException : Could not load type 'Dev.MediaLoader.Domain.MediaUpload' from assembly 'MediaLoader, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.
at Dev.MediaRepository_Fixture.can_add_new_product()
Left by Steve on Mar 01, 2009 11:55 PM

# re: Your first NHibernate based application

Requesting Gravatar...
I can't thank you enough cos' I've searching for a week to learn this and your article made it in a twinkling of an eye.

So I followed this and everything went along just fine.

I wanted to add an extra class (class user). So:

- In FirstSolution.Domain, I added : IUserInterface.cs and User.cs

- In FirstSolution.Mapping, I added: User.hbm.xml

- In FirstSolution.Repositories, I added UserRepository.cs

- In FirstSolution.Repositories.NHibernateHelper.cs, I added also this line:
configuration.AddAssembly(typeof(User).Assembly);


When I run the test, I face the following error:
***** FirstSolution.Tests.GenerateSchema_Fixture.Can_generate_schema
16:04:43,671 ERROR [TestRunnerThread] Configuration [(null)]- Could not compile the mapping document: FirstSolution.Mappings.Product.hbm.xml
NHibernate.MappingException: Could not compile the mapping document: FirstSolution.Mappings.Product.hbm.xml ---> NHibernate.DuplicateMappingException: Duplicate class/entity mapping FirstSolution.Domain.Product

at NHibernate.Cfg.Mappings.AddClass(PersistentClass persistentClass)

Would you help please, I started using your sample to develop some code.
Left by Great!! One question... on Mar 13, 2009 11:35 AM

# re: Your first NHibernate based application

Requesting Gravatar...
Hi again, I posted the "GreAt!! one question" post.

Just to say I also tried to put all new code (user related) in the same files (correponding product files). and I have the same error.

I'll keep on trying cos I have no other choice, this tutorial is the best I ever found.

Thx
Left by Gaby on Mar 16, 2009 7:49 AM

# Nicely Done

Requesting Gravatar...
This article REALLY helped me out.. Very well written and just the right mix of details versus "seeing it work".

Thanks a lot Gabriel, I really appreciate you taking the time out to write this article.
Left by Rob Cooper on Mar 23, 2009 11:32 AM

# re: Your first NHibernate based application

Requesting Gravatar...
@Great...: the exception clearly states that there is a duplicate entry in the mapping files. Obviously you have some copy past error in the xml. please double check your hbm's
Left by Gabriel N. Schenker on Apr 05, 2009 11:52 AM

# re: Your first NHibernate based application

Requesting Gravatar...
This article is fantastic. Really helpful.
Thank you very much !
Left by Alex Jarnoux on Apr 10, 2009 1:45 AM

# re: Your first NHibernate based application

Requesting Gravatar...
Thank you for this excellent tutorial. I found it extremely helpful to get going with nHibernate and TDD. Thanks again, Mark
Left by Mark Levinson on May 05, 2009 10:57 AM

# re: Your first NHibernate based application

Requesting Gravatar...
Good article ;-)

Gabriel Schenker, have you got an article about connecting to MySQL? I have some problems with it but didn't found working samples.
Left by Chiz on May 08, 2009 2:56 AM

# re: Your first NHibernate based application

Requesting Gravatar...
Hi! Great article, but I can't get it working...

Though I have the same code as you, but I get the following error:
'FirstSolution.Domain.Product.Id.get' must declare a body because it is not marked abstract of extern.
I have the same code as you have in Product.cs, am I missing something?
Left by AVARD on May 08, 2009 3:33 AM

# re: Your first NHibernate based application

Requesting Gravatar...
This tutorial so far is great. I am using the built in VS2008 Test project type instead of NUnit and for the life of me I can't the test project to load the SqlServerCE assembly:

Test method FirstSolution.Tests.GenerateSchema_Fixture.Can_generate_schema threw exception: NHibernate.HibernateException: Could not create the driver from NHibernate.Driver.SqlServerCeDriver. ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> NHibernate.HibernateException: The IDbCommand and IDbConnection implementation in the assembly System.Data.SqlServerCe could not be found. Ensure that the assembly System.Data.SqlServerCe is located in the application directory or in the Global Assembly Cache. If the assembly is in the GAC, use element in the application configuration file to specify the full name of the assembly..

My assembly is referenced and copy local = true. It is also in the run dir of test results and the bin\debug dir of the test project...help!
Left by Daryl Hemeon on May 11, 2009 3:29 PM

# re: Your first NHibernate based application

Requesting Gravatar...
I redid the all tests using NUnit and I am receiving the same result as above:

----> System.Reflection.TargetInvocationException : Exception has been thrown by the target of an invocation.
----> NHibernate.HibernateException : The IDbCommand and IDbConnection implementation in the assembly System.Data.SqlServerCe could not be found. Ensure that the assembly System.Data.SqlServerCe is located in the application directory or in the Global Assembly Cache. If the assembly is in the GAC, use element in the application configuration file to specify the full name of the assembly.
Left by Daryl Hemeon on May 12, 2009 9:45 AM

# re: Your first NHibernate based application

Requesting Gravatar...
I had the wrong install of the SqlServer Compact Framework...apparently...
Left by Daryl Hemeon on May 12, 2009 10:10 AM

# re: Your first NHibernate based application

Requesting Gravatar...
Hi first of all thanks for the nice tutorial but i'm stucked...

I get the next error and dont know how to fix it :(
NHibernate.Bytecode.UnableToLoadProxyFactoryFactoryException: Unable to load type 'NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle' during configuration of proxy factory class.
Possible causes are:
- The NHibernate.Bytecode provider assembly was not deployed.
- The typeName used to initialize the 'proxyfactory.factory_class' property of the session-factory section is not well formed.


Plz help me out? More information plz how does this come ?
Left by Joris on May 19, 2009 7:20 AM

# re: Your first NHibernate based application

Requesting Gravatar...
Hi,
I am using Vs 2008 and my hibernate.cfg.xml is as follows




NHibernate.Driver.SqlClientDriver
NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle

"Data Source=xxx;Initial Catalog=xxx;Integrated
Security=SSPI;"

10
false
NHibernate.Dialect.MsSql2000Dialect
true
60
true 1, false 0, yes 'Y', no 'N'




when I run the app I keep on getting the following error and don't know how to solve it

NHibernate.Bytecode.UnableToLoadProxyFactoryFactoryException: Unable to load type 'NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle' during configuration of proxy factory class.
Possible causes are:
- The NHibernate.Bytecode provider assembly was not deployed.
- The typeName used to initialize the 'proxyfactory.factory_class' property of the session-factory section is not well formed.
can you pls pls help me
Left by Venkatesh on May 22, 2009 4:38 PM

# re: Your first NHibernate based application

Requesting Gravatar...
Thank you very much for a most excellent tutorial. Everything works with NHibernate 2.01 and MsSql Server 2005 (also thanks to comments left here from other users).

I have tried NHibernate several times but this is the first time with total success.

Thanks again.
Left by Stig Benning on May 27, 2009 5:37 AM

# re: Your first NHibernate based application

Requesting Gravatar...
As a domain should be PI I would suggest that you define the mappingfiles in its own external library. I have started using NHibernate for the first time, and it was no a hard job to get it to work. It takes only one second to define the mappinghandling in the cfg.xml file.
Left by club penguin on Jun 02, 2009 7:26 PM

# re: Your first NHibernate based application

Requesting Gravatar...
Thanks for this amazing tutorial,

I get the following error run the Add test:
UKIL.Client.NHibernate2Ce.Tests.ProductRepository_Fixture (TestFixtureSetUp):
System.TypeInitializationException : The type initializer for 'NHibernate.Proxy.CastleProxyFactory' threw an exception.
----> System.IO.FileNotFoundException : Could not load file or assembly 'Castle.DynamicProxy, Version=1.1.5.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc' or one of its dependencies. The system cannot find the file specified.

Am I missing something?

I followed the tutorial step by step, seems that I have all the files required.
Left by Ilan on Jun 03, 2009 4:59 AM

# re: Your first NHibernate based application

Requesting Gravatar...
Using NH2.1 Alpha3

Like others said in the new version of NHibernate some updates:
Choose factory (castle,linfu, or spring) and deploy it:
For example, copy NHibernate.ByteCode.Castle.dll in SharedLibs (so it's deployed with post build - see above)
Configure the factory according to the factory provider in the hibernate config, like the following:
NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle
read this <nhforge.org/.../nh2-1-0-bytecode-providers.aspx>

Another update is the use of ExportSchema(cfg).Execute(...) which has new parameters,
I changed it to
ExportSchema(cfg).Create(true/*bool script*/,true /*bool export*/)
Now I have the Add test working ...
Left by Ilan on Jun 03, 2009 6:07 AM

# re: Your first NHibernate based application

Requesting Gravatar...
Great tutorial!
Works with a bit of tweaking using VS2008Team and MySql 5.1.30_community. (no nUnit, using VS2k8 for tests)

Really appreciate the time and effort you put into this Gabriel Schenker.

Steve
Left by steveB on Jun 12, 2009 4:53 AM

# re: Your first NHibernate based application

Requesting Gravatar...
Thanks for the very useful article. I have referenced your article here : haditeo.wordpress.com/.../nhibernate-schemaexport/
Left by Hadi Teo. on Jun 12, 2009 7:04 PM

# re: Your first NHibernate based application

Requesting Gravatar...
Fantastic article! Can't wait to get up and running with Nhibernate in my own apps.
Left by DanB on Jun 17, 2009 11:27 PM

# re: Your first NHibernate based application

Requesting Gravatar...
Great article, thank you.
Left by Kuppu Sammy on Jun 27, 2009 9:29 PM

# re: Your first NHibernate based application

Requesting Gravatar...
Thanks for the tutorial, and the feedback to my previous question. All is well now and i have it up and running... for the most part.

However, my system disagrees with the lines;

[code]
var products = session
.CreateCriteria(typeof(Product))
.Add(Restrictions.Eq("Category", category))
.List();
return products;
[/code]

Namely, it cannot find "Restrictions". More-over, the link you provide to further HQL documentation does not mention "Restriction" at all. Nor do any of my google searches show anything involving HQL and the word 'Restrictions'.

What i did find in the documentation however was the "Expression", located in the 'NHibernate.Expression' namespace. As a result i have changed the line of code to;

.Add(Expression.eq("ParentCatagoryID", "ID"))

as i only want values from the db returned where the 'PK_id' column is identical to the 'ParentCatagoryID' (I.E the catagory is not a sub-catagory of another)

So far my tests build fine. But i am notoriously bad at writing solid tests and so i ask you this.

Have a made a noob-mistake with the Restrictions keyword and missed a using statement somewhere or has Restrictions been depreciated since you wrote this ? Can i expect identical behaviour from using Expressions.eq as when using Restrictions.eq ?

Thank you for your time.


Left by David on Jul 03, 2009 9:11 AM

Your comment:

 (will show your gravatar)
 
Please add 7 and 8 and type the answer here: