-->
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.  [ 7 posts ] 
Author Message
 Post subject: programmatically asking Hibernate to recreate the database s
PostPosted: Sun May 25, 2008 9:06 am 
Newbie

Joined: Sun May 25, 2008 9:02 am
Posts: 6
Hello,

I have a problem when asking Hibernate to programmatically recreate the database schema.


If I define this property in hibernate.cfg.xml
Code:
  <property name="hbm2ddl.auto">create</property>

Then, this code works ok:
Code:
  Configuration config = new Configuration().configure();
  SessionFactory sessionFactory = config.buildSessionFactory();
  sessionFactory = hibernateConfiguration.buildSessionFactory();


However, if I remove that line from hibernate.cfg.xml
Then, this code does not work ok:
Code:
  Configuration config = new Configuration().configure();
  config.setProperty("hbm2ddl.auto", "create");
  SessionFactory sessionFactory = config.buildSessionFactory();
  sessionFactory = hibernateConfiguration.buildSessionFactory();


That is,
if I programmatically ask Hibernate to create the database schema,
Hibernate does not do it.

What can be the problem?

Many thanks,
DAvid


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 25, 2008 9:36 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Hi, I think you should .setProperty("hbm2ddl.auto", "create") before calling configure();

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 25, 2008 10:08 am 
Newbie

Joined: Sun May 25, 2008 9:02 am
Posts: 6
s.grinovero wrote:
Hi, I think you should .setProperty("hbm2ddl.auto", "create") before calling configure();


I need to configure Hibernate from the file "hibernate.cfg.xml".
Except, that I need to override the "hbm2ddl.auto" property from that file.

So, I need to call first config.configure() to read the file "hibernate.cfg.xml".
And then to override that property.


I should be able to call config.setProperty("hbm2ddl.auto", "create") afterwards, isn't it?



DAvid


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 25, 2008 10:40 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
yes you are right, please forget about my previous post.
it appears configure() is just reading the file, I thought it also did some initialization, so I'm wrong.

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 25, 2008 10:44 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
I am using
Code:
cfg.setProperty( org.hibernate.cfg.Environment.HBM2DDL_AUTO, "create" );

in one of my projects and it is working; it turns out that the HBM2DDL_AUTO constant resolves to
Code:
"hibernate.hbm2ddl.auto"

You are having the "hibernate." missing ;-)

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 25, 2008 10:50 am 
Newbie

Joined: Sun May 25, 2008 9:02 am
Posts: 6
s.grinovero wrote:
I am using
Code:
cfg.setProperty( org.hibernate.cfg.Environment.HBM2DDL_AUTO, "create" );

in one of my projects and it is working; it turns out that the HBM2DDL_AUTO constant resolves to
Code:
"hibernate.hbm2ddl.auto"

You are having the "hibernate." missing ;-)


That's working, thanks! :)

DAvid


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 25, 2008 2:43 pm 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
To approach this from a slightly different angle, you might even want to look at the SchemaExport class for creating the underlying database tables, as opposed to simply setting a property on the Configuration object. The SchemaExport class gives you a little bit more control over when the database will get generated, as opposed to simply regenerating when the Configuration object of Hibernate is initialized.

Hibernate.org JavaDoc for SchemaExport Object:

Image

http://www.hibernate.org/hib_docs/v3/api/org/hibernate/tool/hbm2ddl/SchemaExport.html

Here's a simple little Hibernate3 example that uses the SchemExport class to programatically generate a database based on the various classes that make up the Configuration:

Code:
  public static void main(String args[]) {
    AnnotationConfiguration config =
                 new AnnotationConfiguration();
    config.addAnnotatedClass(User.class);
    config.configure();
    new SchemaExport(config).create(true, true);
  }


This example can be see in more detail in this Hibernate3 and JPA tutorial:

http://www.hiberbook.com/HiberBookWeb/learn.jsp?tutorial=02validatingthehibernateenvironment

The other nice thing about the SchemaExport and SchemaUpdate objects is that they have corresponding ANT tasks, namely, SchemaExportTask and SchemaUpdateTask.

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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.