jQuery in Action, 3rd edition
The moose likes Object Relational Mapping and the fly likes How do I use hbm2ddl correctly? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "How do I use hbm2ddl correctly?" Watch "How do I use hbm2ddl correctly?" New topic

How do I use hbm2ddl correctly?

Ylva Degerfeldt
Ranch Hand

Joined: Feb 12, 2008
Posts: 30
Hello everyone,

I'm still a newbie at using Hibernate and now I'm trying to use the automatic schema generation feature (this thing called "hbm2ddl"), but for some reason it's not working so I'd like to know the correct way of doing it.

I'm using Hibernate3, MySQL and NetBeans 5.5.1 in an ordinary Java application.

I've written my Java POJOs, their mapping hbm.xml files and I've put (among other things) these lines in my hibernate.cfg.xml file:

I've also mapped the hbm.xml files to the cfg.xml file.

I thought I didn't have to do anything more than this to use the automatic schema generation. "Hibernate in Action" says that when using the "create" value of hbm2ddl, the database schema should be generated as soon as you've executed buildSessionFactory().

But I Am creating a SessionFactory in a static init method I have in my HibernateUtil class (according to many examples).

The problem is that nothing is happening. No database is showing up in NetBeans or in the MySQL console.

At least nothing was happening when I only tried to open and close a Session in Main (to try it out).

Then I added some code for manually trying to use that SchemaExport class like this:

And in Main I added calls to beginTransAction.
But after this, when I tried to run the application (more specifically in the call to session.beginTransaction())I got these exceptions:

"org.hibernate.exception.GenericJDBCException: Cannot open connection
Caused by: java.sql.SQLException: Connections could not be acquired from the underlying database!"

..which proves to me that the database has not been created.(Anyway, it's not shown anywhere.)

Am I supposed to add something to NetBeans's build-impl.xml or something to make this work?

Please help me! I'm lost.

Thanks in advance.

Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968

In MySQL, Hibernate will create the database, but you need to create the schema first:

Here's a little tutorial that will help you get started with Hibernate, and have Hibernate create the various tables for you. I think you'll find it helpful:

Setting Up a Java Hibernate Development Environment with MySQL on Windows XP

-Cameron McKenzie
Ylva Degerfeldt
Ranch Hand

Joined: Feb 12, 2008
Posts: 30
Thank you, Cameron!

I created the database with an SQL command and after that I was able to generate some tables..

But the big problem is: only TWO tables out of four are generated. (Tell me if I should post this in a new thread. For the moment I'm putting it here.)
This is really weird because I See (in the console) that the "CREATE TABLE" and "ALTER TABLE" commands are run for all four tables, but later when I check in MySQL only two tables are there.

I'm getting no exceptions. That's also weird.

I'm wondering if this problem has anything to do with that I have composite-id mappings (including FK's) in some hbm.xml files and that they may not be correct. Could that be the case?

I tried to find this out by changing one of the hbm.xml files into not having any composite ids or foreign keys but then I got another problem:
Even after "clean and build" the table creation still tried to create the old version of that table.

Do I have to do something extra to tell hbm2ddl that I've made changes to the hbm.xml files?

Sorry for asking so many questions at once but I'm desperate and I Really Need help with this! (I have an extremely important deadline that I have to follow.)

Please help me!

I agree. Here's the link: http://aspose.com/file-tools
subject: How do I use hbm2ddl correctly?
It's not a secret anymore!