Announcement Announcement Module
Collapse
No announcement yet.
Trying to Pass Hibernate Connection Properties Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Trying to Pass Hibernate Connection Properties

    I am getting the following error while trying to pass in values from a properties file to Spring, so that I don't have to supply them directly in hibernate.cfg.xml. Is there a better (and correct) approach? I know that the properties file is being referenced because if I put in in invalid password, it fails on that.

    I have obviously missed an important step here, but I cannot find it.

    I'd be grateful for any help.



    Code:
    Apr 17, 2012 2:44:28 PM org.hibernate.connection.UserSuppliedConnectionProvider configure
    WARNING: No connection properties specified - the user must supply JDBC connections
    Exception in thread "main" org.hibernate.HibernateException: Hibernate Dialect must be explicitly set
    	at org.hibernate.dialect.DialectFactory.determineDialect(DialectFactory.java:57)
    	at org.hibernate.dialect.DialectFactory.buildDialect(DialectFactory.java:39)
    ApplicationContext.xml

    Code:
      <bean id="propertyConfigurer"
              class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="location">
                <value>jdbc.properties</value>
            </property>
        </bean>      
    
        <context:component-scan base-package="cmsutil"/>
    
        <bean id="dataSource"
              class="org.springframework.jdbc.datasource.DriverManagerDataSource"
              p:driverClassName="${jdbc.driverClassName}"
              p:url="${jdbc.url}"
              p:username="${jdbc.username}"
              p:password="${jdbc.password}"/>
    
        <bean id="sessionFactory"
              class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
              p:dataSource-ref="dataSource"
              p:configurationClass="org.hibernate.cfg.AnnotationConfiguration"
              p:packagesToScan="cmsutil.*">
                  
                  
            <property name="exposeTransactionAwareSessionFactory" value="false" />
                  
            <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                    <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                    <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
                    <prop key="hibernate.generate_statistics">${hibernate.generate_statistics}</prop>
                </props>
            </property>
            
            
            <property name="mappingResources">
                <list>
                    <value>cmsutil/entity/contentcomponent.hbm.xml</value>
                </list>
            </property>
     
            
            <property name="eventListeners">
                <map>
                    <entry key="merge">
                        <bean class="org.springframework.orm.hibernate3.support.IdTransferringMergeEventListener"/>
                    </entry>
                </map>
            </property>
    
        </bean>
    
    
    
        <bean id="txnManager"
              class="org.springframework.orm.hibernate3.HibernateTransactionManager"
              p:sessionFactory-ref="sessionFactory"/>

  • #2
    Please post the full stacktrace. A hint (not related to your problem) you already use the AnnotationSessionFactoryBean so no need to explicitly set the configurationClass to use (saves you a line of xml ).

    Comment


    • #3
      Hi Marten. I appreciate the benefit of your experience and knowledge here. I am a newbie, but I want to do this right. I can get it working using the old HibernateUtil method, but I know that's been deprecated. So anyway, here is my full stacktrace, and the Driver program calling the stack trace.

      Code:
      INFO: Mapping class: cmsutil.entity.ContentComponent -> cmsutil.wp_content_components
      Apr 18, 2012 9:39:35 AM org.hibernate.connection.UserSuppliedConnectionProvider configure
      WARNING: No connection properties specified - the user must supply JDBC connections
      Exception in thread "main" org.hibernate.HibernateException: Hibernate Dialect must be explicitly set
          at org.hibernate.dialect.DialectFactory.determineDialect(DialectFactory.java:57)
          at org.hibernate.dialect.DialectFactory.buildDialect(DialectFactory.java:39)
          at org.hibernate.cfg.SettingsFactory.determineDialect(SettingsFactory.java:426)
          at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:128)
          at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2009)
          at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1292)
          at cmsutil.util.Driver.main(Driver.java:25)
      And here is the driver program:


      Code:
      import cmsutil.dao.ContentComponentDAOImpl;
      import cmsutil.entity.ContentComponent;
      import cmsutil.exceptions.DAOException;
      import org.hibernate.HibernateException;
      import org.hibernate.Query;
      import org.hibernate.Session;
      import org.hibernate.SessionFactory;
      import org.hibernate.cfg.Configuration;
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.context.ApplicationContext;
      import org.springframework.context.support.ClassPathXmlApplicationContext;
      
      public class Driver {
      
          private static SessionFactory sessionFactory;
          @Autowired
          ContentComponentDAOImpl c;
      
          public static void main(String[] args) throws DAOException {
              ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
      
              Configuration cfg = new Configuration();
              sessionFactory = cfg.addClass(cmsutil.entity.ContentComponent.class).buildSessionFactory();   <- line 25 causing the error
              Session session = sessionFactory.openSession();
              org.hibernate.Transaction tx = null;
              ContentComponent blog = null;
              
              try {
                  tx = session.beginTransaction();
                  Query q = session.createQuery("from cmsutil.wp_content_components");
                  tx.commit();
              } catch (HibernateException he) {
                  if (tx != null) {
                      tx.rollback();
                  }
                  throw he;
              } finally {
                  session.close();
              }
          }
      }

      Comment


      • #4
        Ehrm... Why do you want to use spring anyway? Your current code doesn't do anything with spring, basically your configuration is useless, you load it and then do nothing with it...

        Code:
        public class Driver {
        
            private static SessionFactory sessionFactory;
            @Autowired
            ContentComponentDAOImpl c;
        
            public static void main(String[] args) throws DAOException {
                ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
                sessionFactory = ctx.getBean("sessionFactory", SessionFactory.class);
                Session session = sessionFactory.openSession();
                org.hibernate.Transaction tx = null;
                ContentComponent blog = null;
                
                try {
                    tx = session.beginTransaction();
                    Query q = session.createQuery("from cmsutil.wp_content_components");
                    tx.commit();
                } catch (HibernateException he) {
                    if (tx != null) {
                        tx.rollback();
                    }
                    throw he;
                } finally {
                    session.close();
                }
            }
        }

        Comment


        • #5
          But isn't the SessionFactory being instantiated by Spring? Any sugggestions on how I can get myself out of this mess? I found this example on the web. Maybe it was "faulty".

          I guess another way to ask the question is, do you know of an example I could follow that is similar to what I am trying to do?

          Thanks again for your time.
          Last edited by jcman01; Apr 18th, 2012, 11:15 AM.

          Comment


          • #6
            Have you actually READ my post and looked at the code I posted?!

            But isn't the SessionFactory being instantiated by Spring?
            Yes it is and afterwards you yourself are doing it again. So basically your whole spring setup is useless.

            Comment


            • #7
              Sorry if this reply is duplicate. I saw the code you posted, but I thought you were just re-stating mine. I tried it, and it worked! Thank you so much. I am getting a small mapping error, but this is certainly a quantum leap.

              Comment


              • #8
                BTW, just pre-ordered your book on Amazon.

                Comment

                Working...
                X