如果我没有以编程方式设置任何内容,而只是调用Configuration configuration = new Configuration().configure();并使用hibernate.properties(如下所示),那么一切都将很好。尝试以编程方式提供用户名,密码和连接URL时,我会收到奇怪的异常提示,提示是hbm文件。我想念什么?
Configuration configuration = new Configuration().configure();
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
*现在 *的hibernate.properties 是
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
编码
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());
例外
我现在得到这个异常,mapping resource我的hbm文件中的每个条目都有一个异常。
mapping resource
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
如果我全部使用hibernate.properties并且没有代码(代码中没有.setProperty),那么一切都会很好。如果使用部分hibernate.properties代码(服务器,用户名,密码),则每个映射属性的hbm都会出错。
hibernate.properties
我需要有人帮助我弄清楚我所缺少的。它应该是真正的基础。
哇,刚刚解决了这个问题。
sessionFactory = configuration.buildSessionFactory(new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry());
我错过了
.applySettings(configuration.getProperties())
学问
hibernate.connection.url
connection.url
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!
http://www.hibernate.org/dtd/
最后,参考此内容以解决此问题。最后的建议Bill Gorder是极好的。
Bill Gorder
private static SessionFactory configureSessionFactory() throws HibernateException { Configuration configuration = new Configuration(); configuration.configure(); ServiceRegistry serviceRegistry = new ServiceRegistryBuilder() .applySettings(configuration.getProperties()) .buildServiceRegistry(); return configuration.buildSessionFactory(serviceRegistry); }