-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: hibernate.hbm2ddl.auto=update deletes data?? (nhibernate)
PostPosted: Tue Aug 21, 2007 12:23 pm 
Newbie

Joined: Tue Aug 21, 2007 12:13 pm
Posts: 3
Hi all,

I'm trying to figure out why update option for hibernate.hbm2ddl.auto is deleting my data when I create a new sessionfactory. Is this the expected behavior? I was under the assumption that update would only update ddl as necessary, currently there are no schema changes necessary but when I create a new session factory the data is gone from the mapped tables!!

Thanks ahead of time and thanks for providing a great product,
Ben


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 21, 2007 2:14 pm 
Expert
Expert

Joined: Fri May 13, 2005 11:13 am
Posts: 292
Location: Rochester, NY
hbm2ddl is not intelligent enough to diff the existing schema with the new one to create the appropriate 'alter' statements, or do something like what SQL Compare does. It just drops everything and creates it anew (or, optionally, just drops everything). I haven't found it to be useful outside of a testing scenario, or for building ddl to be included as part of an installation script.

I usually call it programmatically in my unit tests. Are there configuration-property options for only running hbm2ddl on the first run/if there's no schema in the database already?


Top
 Profile  
 
 Post subject: So does this mean the "update" option doesn't work
PostPosted: Wed Aug 22, 2007 7:45 am 
Newbie

Joined: Tue Aug 21, 2007 12:13 pm
Posts: 3
What option should I use in testing? and if you could list specifically what the actual options are and what they do.

thanks much,
Ben


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 22, 2007 11:27 am 
Expert
Expert

Joined: Fri May 13, 2005 11:13 am
Posts: 292
Location: Rochester, NY
Well, this is interesting. What version are you using? Why I ask:

I've never used the config settings to enable hbm2ddl, so I looked in the docs, which only show

create - drops and creates schema at BuildSessionFactory() (specifically, in the SessionFactoryImpl ctor) and
create-drop - like create, but also drops the schema at ISessionFactory.Close().

Curious as to the 'update' option, I looked in the source (1.2.0 GA) and setting the 'update' option sets an IsAutoUpdateSchema in the settings object, but it doesn't do anything. Here is the excerpt:

Code:
         if (settings.IsAutoCreateSchema)
         {
            new SchemaExport(cfg).Create(false, true);
         }

         /*
         if ( settings.IsAutoUpdateSchema )
         {
            new SchemaUpdate( cfg ).Execute( false, true );
         }
         */

         if (settings.IsAutoDropSchema)
         {
            schemaExport = new SchemaExport(cfg);
         }


The commented 'SchemaUpdate' object does not exist. So, at least for that version, setting it to 'update' should do nothing.

As to your actual question: I would use 'create': it will give you a clean slate every time you open an ISessionFactory, but it will also leave the data if you want to look it over after a particular test.


Top
 Profile  
 
 Post subject: Thanks much
PostPosted: Wed Aug 22, 2007 11:31 am 
Newbie

Joined: Tue Aug 21, 2007 12:13 pm
Posts: 3
Thats what I needed to know!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.