如果我没有以编程方式设置任何东西,而只是调用Configuration configuration = new Configuration().configure();
并使用 hibernate.properties(如下所示),一切都会很好。一旦我尝试以编程方式提供用户名、密码和连接 url,我就会得到一个奇怪的异常,提示 hbm 文件。我错过了什么?
这个作品
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
根据@Kshitij 的建议。做混合模式。
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 文件中的每个条目都有一个。
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 中为每个映射属性得到错误。
我需要有人帮我弄清楚我错过了什么。它应该是非常基本的东西。