-->
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.  [ 4 posts ] 
Author Message
 Post subject: Create new database using hibernate
PostPosted: Sat Jun 14, 2008 1:05 am 
Newbie

Joined: Tue Jun 10, 2008 4:40 am
Posts: 13
Hi,
how to create new database using hibernate while program is running ?
thx.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jun 14, 2008 6:56 am 
Beginner
Beginner

Joined: Tue Aug 30, 2005 2:33 am
Posts: 22
hbm2ddl.auto hibernate property can be set to create,update,create-drop,false etc


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jun 14, 2008 9:49 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Personally, I love using the SchemaExport class. It's got a very simple create method:

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

Quote:
public void create(boolean script, boolean export)
Run the schema creation script.

Parameters:
script - print the DDL to the console
export - export the script to the database


Image

THe code for running it couldn't be easier:


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


You just need to ensure you have a valid AnnotationConfiguration/Hibernate Configuration object initialized properly and you're set.

Image

Here's a full Java class that has a main method which creates a database table to manage the persistent state of the POJO:


Code:
package com.examscam.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;


import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

@Entity
public class User {

  private Long id;
  private String password;

  @Id
  @GeneratedValue
  public Long getId() {
    return id;
  }

  public void setId(Long id) {
    this.id = id;
  }

  public String getPassword() {
    return password;
  }

  public void setPassword(String password) {
    this.password = password;
  }

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


Here's the full tutorial from my website:

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

_________________
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:
PostPosted: Sat Jun 14, 2008 9:53 am 
Regular
Regular

Joined: Sun Apr 13, 2008 3:04 am
Posts: 71
Location: Bangalore
hbm2ddl.auto would only create the schema and not the database it self.. is there a way create database too.. provided the given user and password has privlages to create one and also database supports new db creation.

Regards,
Nagendra

_________________
Raja Nagendra Kumar,
C.T.O
http://www.tejasoft.com
TejaSoft - Specialists in Code Audit, Unit Testing and Merciless Re-factoring - Engineering Crisis Turnaround Experts


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