1

I want to set the Hibernate properties programmatically few of them using xml file

So far I have done below code but that did not work.

MyHibernateUtil.java (This class is to create Hibernate Session Factory):

          private static org.hibernate.SessionFactory sessionFactory;
          private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
          private static String configFile = CONFIG_FILE_LOCATION;
                    static {
    try {
        
        Properties prop = new Properties();
        prop.setProperty("hibernate.connection.username", userId);
        prop.setProperty("hibernate.connection.password", passCode);
        prop.setProperty("hibernate.connection.url", URL);
                  prop.setProperty("hibernate.connection.driver_class", "oracle.jdbc.driver.OracleDriver");
        prop.setProperty("hibernate.dialect", "org.hibernate.dialect.Oracle9Dialect");
        
        annotationConfiguration.configure(configFile).setProperties(prop);;
        sessionFactory = annotationConfiguration.buildSessionFactory();
      
    } catch (Exception e) {
        System.err.println("%%%% Error Creating SessionFactory %%%%");
        e.printStackTrace();
    }
}

     public static org.hibernate.SessionFactory getSessionFactory() 
     //This method will return SessionFactory object
    {
    return sessionFactory;
}

and in my hibernate.cfg.xml I kept following

hibernte.cfg.xml:

   <session-factory>
<property name="dialect">
    org.hibernate.dialect.Oracle9Dialect
</property>
<property name="connection.driver_class">
    oracle.jdbc.driver.OracleDriver
</property>
<property name="hibernate.current_session_context_class">thread</property>
  <property name="show_sql">true</property>

<mapping class="Class1" />
<mapping class="Class2" />
<mapping class="Class3" />

   </session-factory>

I tried this code with console application that is working fine, but When I try to use this code in my Web application I am getting some errors as per my understanding web application is unable to create the object of my HibernateUtil.java.

So I have created a Listener class and in that class I have tried to create SessionFactory Object and then luckily I got SessionFactory object

But My problem here is

When I try to access my web application using URL I got following exception

 14:52:04,734  WARN UserSuppliedConnectionProvider:46 - No connection properties specified - the user must supply JDBC connections
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)
    Error! Please, check your JDBC/JDNI Configurations and Database Server avaliability. 
   Could not open or put a Hibernate Session in ValueStack: Hibernate Dialect must be explicitly set
    org.hibernate.HibernateException: Hibernate Dialect must be explicitly set
at org.hibernate.dialect.DialectFactory.determineDialect(DialectFactory.java:80)
at org.hibernate.dialect.DialectFactory.buildDialect(DialectFactory.java:62)
at org.hibernate.cfg.SettingsFactory.determineDialect(SettingsFactory.java:460)
at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:155)
at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2101)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1325)
at  org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:867)
at com.googlecode.s2hibernate.struts2.plugin.util.HibernateSessionFactory.rebuildSessionFactory(HibernateSessionFactory.java:99)
at com.googlecode.s2hibernate.struts2.plugin.interceptors.SessionTransactionInjectorInterceptor.getHibernateSessionFromFactory(SessionTransactionInjectorInterceptor.java:289)
at com.googlecode.s2hibernate.struts2.plugin.interceptors.SessionTransactionInjectorInterceptor.intercept(SessionTransactionInjectorInterceptor.java:125)
   at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242)
   at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:54)
  at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:544)
  at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at   org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1002)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)

Below are my web application capabilities:

  • Struts 2.3.x
  • HIbernate 3.1.x

Edit:

Same Problem Solution not found

I too have same kind but I cant find

The following class

   ServiceRegistryBuilder
   ServiceRegistrry 

 Are these classes are from Hibernate or we need to add any third party lib.
Roman C
  • 49,761
  • 33
  • 66
  • 176
Rookie007
  • 1,229
  • 2
  • 18
  • 50

1 Answers1

0

You should specify location of the hibernare.cfg.xml when building a session factory.

/**
 * Location of hibernate.cfg.xml file.
 * Location should be on the classpath as Hibernate uses
 * #resourceAsStream style lookup for its configuration file.
 * The default classpath location of the hibernate config file is
 * in the default package. Use #setConfigFile() to update
 * the location of the configuration file for the current session.
 */
private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";


annotationConfiguration.configure(CONFIG_FILE_LOCATION).setProperties(prop);
Roman C
  • 49,761
  • 33
  • 66
  • 176
  • I specified the file path. Cause I can see in my console when calling my listner class All mapping classes are logged. – Rookie007 Jun 30 '14 at 07:24
  • You said _I want to set the hibernate properties programmatically..._ – Roman C Jun 30 '14 at 07:31
  • Yes some of them like username and password by program rest of them by xml file Can this possible ? Mixed properties settings .. ? – Rookie007 Jun 30 '14 at 07:32
  • May be somewhere I am missing.. I have done what u have suggested. I mean this line `annotationConfiguration.configure(CONFIG_FILE_LOCATION).setProperties(prop);` Before I call `annotationConfiguration.buildSessionFactory();` this line. – Rookie007 Jun 30 '14 at 07:37
  • I haven't seen in your code you provide a config file, your config file contains configuration settings necessary to build a session factory. – Roman C Jun 30 '14 at 07:39
  • Okay i will edit my question again. Please let me know where I am missing. Thanks – Rookie007 Jun 30 '14 at 08:09
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/56555/discussion-between-looser-and-roman-c). – Rookie007 Jun 30 '14 at 15:43
  • Thanks for your time.. I dont know whats causing the problem . But I have upgraded to Hibernate 4.3.5. Now I can do Mixed properties . Thanks – Rookie007 Jul 02 '14 at 03:28
  • The problem was you have used `configuartion()` without a parameter for configuration file. – Roman C Jul 05 '14 at 09:11