• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generating DDL from hibernate

 
Jane Jukowsky
Ranch Hand
Posts: 157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Woohoo! I just discovered you can do that!


Configuration cfg = HibernateSessionFactory.getConfiguration();
String[] lines = cfg.generateSchemaCreationScript(new DerbyDialect());

Result:

create table CLASSICCARS.ECHO_MESSAGE (ID bigint not null, MSG varchar(255), primary key (ID));


Small problem: I already have my schemas and don't want to drop them. So I thought I'll do update:

Configuration cfg = HibernateSessionFactory.getConfiguration(); //new Configuration();
Session s = HibernateSessionFactory.getSession();
Connection c = s.connection();

DatabaseMetadata m = new DatabaseMetadata(c, new DerbyDialect(), true);
String[] lines = cfg.generateSchemaUpdateScript( new DerbyDialect(), m);//new PostgreSQLDialect());


So far so good, right? But lines are empty. What am I doing wrong? And WHY in the world is there a difference in signature between update and create?

While I am there: since it's capable of doing that, is there any way to have hibernate create table columns on the fly when my model changes? My model does it often, and so far and hibernate does not seem to be a very good.. how do I put it.. prototyping language.


Thanks
 
Jane Jukowsky
Ranch Hand
Posts: 157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

OK, I figured out why my lines were zero and why different signature: my database was in synch and no update was needed; the update method compares config to an arbitrary database, which may or may not be standard database.

I added a column to the model, and now the update method generated this for me:

create table CLASSICCARS.ECHO_MESSAGE (ID bigint not null, MSG varchar(255), MY_DESCRIPTION varchar(255), primary key (ID));


That's nice, but that's a create, not an alter! Any way I can get it to create an alter?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic