-->
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.  [ 2 posts ] 
Author Message
 Post subject: Create entire database at RUNTIME using hibernate-tools jars
PostPosted: Sat Mar 01, 2008 12:55 pm 
Newbie

Joined: Sat Mar 01, 2008 12:42 pm
Posts: 2
My standalone JSE application, which currently uses hibernate 3.2.5, needs to check if the schema exists in the database, and if not, then generate the entire database.

This was previously being accomplished with a gigantic static sql query for creating the tables. If the schema isn't there upon startup of application, then we'd use straight JDBC to fire this static sql and create the tables. It worked alright.

However, if the domain object changes, then we have to update this giant static line of SQL to reflect these changes and accurately generate the schema.

I'm aware of a tool called hbm2ddl which can be used with ANT to create the database at BUILD time. However, I absolutely need to be able to do this at RUNTIME.

Is it possible to utilize some of the jar files in Hibernate Tools, to actually generate the schema from the hbm mappings, and create the tables at runtime??? Has it been done before?? I've done lots of searching through hibernate forums and google, and although creating the schema at build time is very common, nobody seems to have a need to do it at runtime.

The reason this must be done at runtime is that it is common for the database of this application to actually be deleted by the customer every so often. The app simply needs to be sensitive to this, and then use hibernate to generate the database tables again.

Any ideas? anyone know of any tutorials on this at least?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 01, 2008 5:34 pm 
Newbie

Joined: Sat Mar 01, 2008 12:42 pm
Posts: 2
UPDATE:


SchemaExport is a class I found while browsing the hibernate tools API:

org.hibernate.tool.hbm2ddl.SchemaExport;

I'm trying something like this right now (not working):

Code:
DAOHibernateImpl dao = DAOHibernateImpl.getInstance();
boolean isDatabaseCreated = dao.isDatabaseCreated();
if (!isDatabaseCreated) {
    System.out.println("attempting create");
    SchemaExport exporter =
    new SchemaExport(
        new Configuration().configure("hibernate.cfg.xml"));
    exporter.create(true, true);      
}


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