Home >

NHibernate SchemaValidator

23. November 2008

NHibernate provides a number of tools for developers to manage their database. I prefer mapping driven approach in which I let NHibernate generate the schema for database for me. By that way, I only concentrate on my domain. For me, database is not the center but only a tool for storage most of the cases.

This approach is valid only for greenfield projects. If you’re using a legacy database, however, things get more complicated and you write your mappings according to your database.

NHibernate now(with revision 3918, which means you have to use trunk until we release it) provides a way to verify your mappings against your database.

The usage is similar to other tools

I won’t write a dedicated code for this but rather I am going to copy the tests from the code itself.

[TestFixture]
public class SchemaValidateFixture
{
    [Test]
    public void ShouldVerifySameTable()
    {
        string resource1 = "NHibernate.Test.Tools.hbm2ddl.SchemaValidator.1_Version.hbm.xml";
        Configuration v1cfg = TestConfigurationHelper.GetDefaultConfiguration();
        using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resource1))
        new NHibernate.Tool.hbm2ddl.SchemaExport(v1cfg).Execute(true,true,false,true);

        var v1schemaValidator = new NHibernate.Tool.hbm2ddl.SchemaValidator((v1cfg));
        v1schemaValidator.Validate();
    }
    [Test]
    public void ShouldNotVerifyModifiedTable()
    {
        string resource1 = "NHibernate.Test.Tools.hbm2ddl.SchemaValidator.1_Version.hbm.xml";
        string resource2 = "NHibernate.Test.Tools.hbm2ddl.SchemaValidator.2_Version.hbm.xml";
        Configuration v1cfg = TestConfigurationHelper.GetDefaultConfiguration();
        Configuration v2cfg = TestConfigurationHelper.GetDefaultConfiguration();
        using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resource1))
            v1cfg.AddInputStream(stream);
        using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resource2))
            v2cfg.AddInputStream(stream);
        new NHibernate.Tool.hbm2ddl.SchemaExport(v1cfg).Execute(true, true, false, true);
        var v2schemaValidator = new NHibernate.Tool.hbm2ddl.SchemaValidator((v2cfg));
        try
        {
            v2schemaValidator.Validate();                
        }
        catch (HibernateException e)
        {
            Assert.That(e.Message, Text.StartsWith("Missing column: Name"));
        }
    }
}

SchemaValidator has a single method called Validate. You give it the configuration and it validates the schema.

That’s it. In the next post I’ll write about an unknown configuration option for NHibernate.

kick it on DotNetKicks.com

Comments

11/23/2008 1:41:53 PM #
Trackback from DotNetKicks.com

NHibernate SchemaValidator
11/23/2008 2:05:03 PM #
Trackback from NHibernate blog

NHibernate hbm2ddl
7/23/2010 10:21:43 PM #
NHibernate hbm2ddl

NHibernate hbm2ddl
3/23/2011 7:57:44 AM #
Hey, i just want to inform you that your website had given me good information. By the way call center Philippines is now a growing industry.
Comments are closed