2

I am using hibernate.properties to configure my hibernate connection to mysql. But I need the mysql server to be configurable programmatically.

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

In code I tried this

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

I get this exception when run a select query programmatically.

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)
Siddharth
  • 9,349
  • 16
  • 86
  • 148
  • 1
    possible duplicate of [Programmatic hibernate configuration after xml configuration](http://stackoverflow.com/questions/5516369/programmatic-hibernate-configuration-after-xml-configuration) – Perception Jan 11 '12 at 15:09
  • Nope, I have hibernate prefix in correctly programmatically. – Siddharth Jan 11 '12 at 15:27
  • 1
    You are blowing away your programmatic configuration because you are doing it before the XML configuration parsing occurs. See the duplicate question I linked for the correct way to code this. – Perception Jan 11 '12 at 15:43
  • So I have modified the code, I still get a exception. Exception in thread "main" java.lang.UnsupportedOperationException: The application must supply JDBC connections 'Configuration configuration = new Configuration().configure() ; configuration.setProperty("hibernate.connection.url", jdbcProperty); sessionFactory = configuration.buildSessionFactory(new ServiceRegistryBuilder().buildServiceRegistry());` – Siddharth Jan 11 '12 at 17:04

1 Answers1

0

If I do all the configuration programmatic, it works. Or I can choose to do all the configuration through xml. But I have to choose 1 of them. Mix does does not work well. Thanks to all for helping out here.

Edit

I got the mixed working. I needed the server, username and pwd to be taken programmatically and reset statically from hibernate.properties.

  • Long Answer : Refer to this
  • Short Answer : I was missing .applySettings(configuration.getProperties()) in

Code

sessionFactory = configuration
    .buildSessionFactory(new ServiceRegistryBuilder()
    .applySettings(configuration.getProperties()) 
    .buildServiceRegistry());
Community
  • 1
  • 1
Siddharth
  • 9,349
  • 16
  • 86
  • 148