If I dont set anything programmatically and just call Configuration configuration = new Configuration().configure();
and use the hibernate.properties (as below) everything works just great. As soon as I try to provide username, password and connection url programmatically, I get a weird Exceptions, hinting at the hbm file. What am I missing ?
hibernate.connection.driver_class=com.mysql.jdbc.Driver
hibernate.connection.url=jdbc:mysql://myEC2/mCruiseOnServerDB?autoReconnect=true&failOverReadOnly=false&maxReconnects=10
hsqldb.write_delay_millis=0
shutdown=true
hibernate.connection.username=root
hibernate.connection.password=mypwd
hibernate.connection.pool_size=2
hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect
hibernate.c3p0.idle_test_period=300
hibernate.c3p0.timeout=120
The hibernate.properties now is
hibernate.connection.driver_class=com.mysql.jdbc.Driver
hsqldb.write_delay_millis=0
shutdown=true
hibernate.connection.pool_size=2
hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect
The Code
String connection = "jdbc:mysql://"
+ Globals.DBSERVER
+ "/mCruiseOnServerDB?autoReconnect=true&failOverReadOnly=false&maxReconnects=10";
Configuration configuration = new Configuration()
.setProperty("hibernate.connection.url", connection)
.setProperty("hibernate.connection.username", Globals.DB_USER_NAME)
.setProperty("hibernate.connection.password", Globals.DB_PASSWORD);
configuration.configure();
sessionFactory = configuration
.buildSessionFactory(new ServiceRegistryBuilder()
.buildServiceRegistry());
The Exception
I now get this exception, one for every mapping resource
entry in my hbm file.
11 May 2013 08:46:31,969 1300 [main] FATAL ReadOnlyOperations - Have chosen to ignore this runtime exception java.lang.UnsupportedOperationException: The application must supply JDBC connections, may be fatal, examine this carefully
11 May 2013 08:46:31,969 1300 [main] FATAL ReadOnlyOperations - java.lang.UnsupportedOperationException: The application must supply JDBC connections
If I use all hibernate.properties
and no code (no .setProperty in code) everything works great. If I use part hibernate.properties
and part code (server, username, password) I get errors for in the hbm for every mapping property.
I need someone to help me figure out what I am missing. It should be something really basic.
Wow, just fixed the problem.
sessionFactory = configuration.buildSessionFactory(new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry());
I was missing the
.applySettings(configuration.getProperties())
Learnings
hibernate.connection.url
and NOT connection.url
if you use hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect
WARN Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
, you need to replace http://www.hibernate.org/dtd/
in the cfg.xml and all hbm files too. Dont forget teh hbm files, they too use the same DTD.Lastly, referred to this, to fix this. The last advice by Bill Gorder
is superb.
private static SessionFactory configureSessionFactory()
throws HibernateException {
Configuration configuration = new Configuration();
configuration.configure();
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
.applySettings(configuration.getProperties())
.buildServiceRegistry();
return configuration.buildSessionFactory(serviceRegistry);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With