-->
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: Create Database at Runtime?
PostPosted: Wed Jun 04, 2008 6:25 pm 
Newbie

Joined: Wed May 28, 2008 11:11 am
Posts: 12
Hi Guys,

I am wanting to create a database at startup (if it is not already present).

The url information in my configuration file (hbm.xml) seems seems to be the url for connecting to a Database (which of course if NOT created yet).

Is there a way to do this?

It seems like I have to connect to the DB Server and then try to create the wanted DB from there but I cannot figure this out.

Any help would be appreciated!
Thanks, Venkman


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 05, 2008 5:14 am 
Regular
Regular

Joined: Wed Apr 09, 2008 10:28 am
Posts: 52
actually i'm intrested in this too i know how to create tables by hibernate but don't get to work that the database is created aswell, if you find a solution please share ;).


Top
 Profile  
 
 Post subject: Create Database at Runtime?
PostPosted: Thu Jun 05, 2008 11:30 am 
Newbie

Joined: Wed May 28, 2008 11:11 am
Posts: 12
Does anyone know if this is possible with Hibernate? If it is possible, then how is it done?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 05, 2008 11:44 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Why not just use the SchemaExport class. The code is pretty simple.


http://jpa.ezhibernate.com/Javacode/learn.jsp?tutorial=02validatingthehibernateenvironment

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


Image

_________________
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  
 
 Post subject: Create Database at Runtime?
PostPosted: Thu Jun 05, 2008 12:31 pm 
Newbie

Joined: Wed May 28, 2008 11:11 am
Posts: 12
Thanks Cameron for replying.
I have a copy of your book and I like it a lot!

I didn't get the gist though on how to actually CREATE a new Database with Hibernate, only populate an existing one with a fresh schema. Is that so?

I am porting an existing application to Hibernate. The old application created a new Database with code like this:

// attempt to create the user specified database
try {
Class.forName(jdbcDriver);

// connect to the default PostgreSQL database
String defaultDatabase = "template1";
String url = jdbcURL + hostName + defaultDatabase;

db = DriverManager.getConnection(url, user, password);
st = db.createStatement();

// create the user specified database. Assigned ownership to
// user specified super user
String command = "CREATE DATABASE \"" + databaseName + "\" WITH OWNER = \"" + user + "\" ENCODING = 'SQL_ASCII' TABLESPACE = pg_default";

st.execute(command);

st.close();
db.close();
}
...


I have been able to create a new schema on an already existing Database but have not been able to create a NEW database on the fly like this code does.

This is what I am after.
Thanks, Venkman


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.