NHibernate Forge
The official new home for the NHibernate community

NHibernate hbm2ddl

NHibernate has a number of configuration options: Enabling statistics, Caching etc. You can check more from here

I will now talk about an unknown feature(well, at least I didn’t know until I implement SchemaValidator): hbm2ddl.auto

Hbm2ddl.auto is declarative way to use SchemaExport / SchemaUpdate / SchemaValidator (well the latter sounds odd, maybe it would be better to call it SchemaValidate, what do you think?). If you add

<property name="hbm2ddl.auto">create</property>

for example, it will run

new SchemaExport(cfg).Create(false, true);

during SessionFactory initialization, with which you probably are familiar.

There are several options for hbm2ddl.auto.

  1. update executes SchemaUpdate which will modify your existing table with new mapping, without dropping any columns.
  2. create-drop executes SchemaExport when SessionFactory initializes and drops the schema at the end of the life of the factory.
  3. validate executes SchemaValidator which I blogged about here

If you prefer not to use programatic way, configuration is just here.


Posted nov 23 2008, 12:04 a.m. by Tuna Toksoz

Comments

sushanty2k5 wrote re: NHibernate hbm2ddl
on 03-15-2009 17:40

I'm using Fluent NH for mapping and NH as ORM in my C# application.. The problem is with schemaupdate class.. if I create an instance of that.. using the configuration I got from my exposeconfiguration function.. It doesnot update the schema.. I need some better documentation on SchemaUpdate class.. or might be.. setting the configuration to accomodate the new schema in an old database.. for versioning purposes..

I have tried a lot of variations but no DDL command is reflected on the actual database using SchemaUpdate :(

is this my problem:

code.google.com/.../detail

?

Tuna Toksoz wrote re: NHibernate hbm2ddl
on 03-15-2009 17:52

Hello sushanty2k5

Can you please raise the issue at

fluent-nhibernate[AT HERE]googlegroups.com

I believe this has been implemented in FNH but not sure.

sushanty2k5 wrote re: NHibernate hbm2ddl
on 03-16-2009 4:24

I already have done that.. I just wanted to know how it works.. as in.. If I expose the configuration from :

 private static ISessionFactory CreateSessionFactory()  

       {

            return Fluently.Configure()

               .Database(SQLiteConfiguration.Standard

                   .UsingFile(basefile))

               .Mappings(m =>                    m.FluentMappings.AddFromAssemblyOf<Program>())

               .ExposeConfiguration(BuildSchema)

               .BuildSessionFactory();

       }

and use it here :

private static void BuildSchema(NHibernate.Cfg.Configuration cfg)

       {

           config = cfg;          

       }

private static void update_the_schema()

    {

      new SchemaUpdate(config).Execute(true, true);        

    }    

//Here config is that static configuration variable obtained from ExposeConfiguration method...  

Adrian wrote re: NHibernate hbm2ddl
on 10-02-2009 15:28

In my tests, the "create-drop" option doesn't reliably drop the tables...

Also, does NH support an import.sql file as described here?

www.jroller.com/.../hibernate_s_hbm2ddl_tool

Powered by Community Server (Commercial Edition), by Telligent Systems