已选择忽略此运行时异常java.lang.UnsupportedOperationException

时间:2012-01-11 15:00:44

标签: java hibernate

我正在使用hibernate.properties来配置我的hibernate连接到mysql。但是我需要以编程方式配置mysql服务器。

hibernate.connection.driver_class=com.mysql.jdbc.Driver
#hibernate.connection.url=jdbc:mysql://serverA/MyDB
hibernate.connection.username=root
hibernate.connection.password=password
hibernate.connection.pool_size=2
hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect

在代码中我尝试了这个

String jdbcProperty = "jdbc:mysql://"+Globals.DBSERVER+"/MyDB" ;
Configuration configuration = new Configuration()                         
    .setProperty("hibernate.connection.url", jdbcProperty) ;
sessionFactory = configuration.                                 
    configure().buildSessionFactory(new ServiceRegistryBuilder(                     
    .buildServiceRegistry());

以编程方式运行select查询时出现此异常。

Exception in thread "main" java.lang.UnsupportedOperationException: The application must supply JDBC connections
at org.hibernate.service.jdbc.connections.internal.UserSuppliedConnectionProviderImpl.getConnection(UserSuppliedConnectionProviderImpl.java:62)
at org.hibernate.internal.AbstractSessionImpl$NonContextualJdbcConnectionAccess.obtainConnection(AbstractSessionImpl.java:276)
at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:299)
at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.getConnection(LogicalConnectionImpl.java:169)
at org.hibernate.engine.jdbc.internal.proxy.ConnectionProxyHandler.extractPhysicalConnection(ConnectionProxyHandler.java:82)
at org.hibernate.engine.jdbc.internal.proxy.ConnectionProxyHandler.continueInvocation(ConnectionProxyHandler.java:138)
at org.hibernate.engine.jdbc.internal.proxy.AbstractProxyHandler.invoke(AbstractProxyHandler.java:81)
at $Proxy4.prepareStatement(Unknown Source)
at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$5.doPrepare(StatementPreparerImpl.java:149)
at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$StatementPreparationTemplate.prepareStatement(StatementPreparerImpl.java:178)
at org.hibernate.engine.jdbc.internal.StatementPreparerImpl.prepareQueryStatement(StatementPreparerImpl.java:147)
at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1739)
at org.hibernate.loader.Loader.doQuery(Loader.java:828)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:289)
at org.hibernate.loader.Loader.doList(Loader.java:2463)
at org.hibernate.loader.Loader.doList(Loader.java:2449)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2279)
at org.hibernate.loader.Loader.list(Loader.java:2274)
at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:332)
at org.hibernate.internal.SessionImpl.listCustomQuery(SessionImpl.java:1588)
at org.hibernate.internal.AbstractSessionImpl.list(AbstractSessionImpl.java:224)
at org.hibernate.internal.SQLQueryImpl.list(SQLQueryImpl.java:156)

1 个答案:

答案 0 :(得分:0)

如果我执行所有配置程序,它都可以。或者我可以选择通过xml进行所有配置。但我必须选择其中一个。混合确实效果不好。感谢大家帮忙。

修改

我得到了混合工作。我需要以编程方式获取服务器,用户名和密码,并从hibernate.properties静态重置。

  • 长答案:请参阅this
  • 简答:我在
  • 中遗失了.applySettings(configuration.getProperties())

代码

sessionFactory = configuration
    .buildSessionFactory(new ServiceRegistryBuilder()
    .applySettings(configuration.getProperties()) 
    .buildServiceRegistry());