Win a copy of Murach's MySQL this week in the JDBC and Relational Databases forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
  • Mikalai Zaikin

How do I use hbm2ddl correctly?

Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

author and cow tipper
Posts: 5009
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!

Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic