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:
Code:
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/my_database</property>
<property name="connection.username">root</property>
<property name="connection.password">MYPASSWORD</property>
<!-- JDBC connection pool (use c3p0) -->
<property name="c3p0.acquire_increment">1</property>
<property name="c3p0.idle_test_period">100</property> <!-- seconds -->
<property name="c3p0.max_size">100</property>
<property name="c3p0.max_statements">10</property>
<property name="c3p0.min_size">10</property>
<property name="c3p0.timeout">100</property> <!-- seconds -->
<property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
<property name = "transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>
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:
Code:
static{
try
{
Configuration conf = new Configuration();
sessionFactory = conf
.configure()
.buildSessionFactory();
new SchemaExport(conf).create(false, true);
}
......
And in Main I added a call to beginTransAction.
But after this, when I tried to run the application (more specifically in the call to session.beginTransaction())I got these exceptions:
Quote:
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.
/Ylva