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?
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!
Space pants. Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop