title: spring hibernate database connection
body: <p>I have a trouble of understanding how I can integrate <code>hibernate.cfg.xml</code> into my Spring app.
  When I created my Spring app, it generated so much xml files which are scattered very where in the application. I try to understand each of very xml; however I couldnt understand how to connect to database with <code>Spring</code> and <code>Hibernate</code>.</p>
  
  <p><strong>Web.xml</strong></p>
  
  <pre><code>&lt;!-- The definition of the Root Spring Container shared by all Servlets 
      and Filters --&gt;
  &lt;context-param&gt;
      &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
      &lt;param-value&gt;/WEB-INF/spring/root-context.xml&lt;/param-value&gt;
  &lt;/context-param&gt;
  
  &lt;!-- Creates the Spring Container shared by all Servlets and Filters --&gt;
  &lt;listener&gt;
      &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
  &lt;/listener&gt;
  
  &lt;!-- Processes application requests --&gt;
  &lt;servlet&gt;
      &lt;servlet-name&gt;appServlet&lt;/servlet-name&gt;
      &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
      &lt;init-param&gt;
          &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
          &lt;param-value&gt;/WEB-INF/spring/app/servlet-context.xml&lt;/param-value&gt;
      &lt;/init-param&gt;
      &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
  &lt;/servlet&gt;
  
  &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;appServlet&lt;/servlet-name&gt;
      &lt;url-pattern&gt;/spring/*&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  </code></pre>
  
  <p><strong>root-context.xml</strong>:</p>
  
  <pre><code>&lt;import resource="db.xml" /&gt;
  </code></pre>
  
  <p>db.xml:
      
      
          
      </p>
  
  <pre><code>&lt;bean id="entityManagerFactory"
      class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
      &lt;property name="jpaVendorAdapter"&gt;
          &lt;bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"&gt;
              &lt;property name="showSql" value="true" /&gt;
              &lt;property name="generateDdl" value="true" /&gt;
              &lt;property name="databasePlatform" value="${db.dialect}" /&gt;
          &lt;/bean&gt;
      &lt;/property&gt;     
  &lt;/bean&gt;
  
  &lt;bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
      destroy-method="close"&gt;
      &lt;property name="driverClassName" value="${db.driver}" /&gt;
      &lt;property name="url" value="${db.url}" /&gt;
      &lt;property name="username" value="${db.username}" /&gt;
      &lt;property name="password" value="${db.password}" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"&gt;
  &lt;/bean&gt;
  
  &lt;tx:annotation-driven /&gt;
  
  &lt;bean
      class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" /&gt;
  </code></pre>
  
  <p>I tried to modify and changed the db.properties file, but it complained of missing hibernate.cfg.xml</p>
  
  <p><strong>hibernate.cfg.xml</strong></p>
  
  <pre><code>&lt;hibernate-configuration&gt;
  &lt;session-factory&gt;
  &lt;property name="connection.driver_class"&gt;org.postgresql.Driver&lt;/property&gt;
  &lt;property name="connection.url"&gt;jdbc:postgresql://127.0.0.1:5432/table&lt;/property&gt;
  &lt;property name="connection.username"&gt;user&lt;/property&gt;
  &lt;property name="connection.password"&gt;password&lt;/property&gt;
  
  &lt;!-- JDBC connection pool (use the build-in) --&gt;
  &lt;property name="connection.pool_size"&gt;1&lt;/property&gt;
  &lt;!-- SQL dialect --&gt;
  &lt;property name="dialect"&gt;org.hibernate.dialect.PostgreSQLDialect&lt;/property&gt;
  &lt;property name="cache.provider_class"&gt;org.hibernate.cache.HashtableCacheProvider&lt;/property&gt;
  &lt;property name="transaction.factory_class"&gt;org.hibernate.transaction.JDBCTransactionFactory&lt;/property&gt;
  
  &lt;!-- thread is the short name for org.hibernate.context.ThreadLocalSessionContext --&gt;
  &lt;property name="current_session_context_class"&gt;thread&lt;/property&gt;
  
  &lt;!-- Set "true" to show SQL statements --&gt;
  &lt;property name="hibernate.show_sql"&gt;false&lt;/property&gt;
  
  &lt;!-- mapping class using annotation --&gt;
  &lt;mapping class="com.isad.model.Users"&gt;&lt;/mapping&gt;
  &lt;/session-factory&gt;
  
  &lt;/hibernate-configuration&gt;
  </code></pre>
  
  <p>And If I include <code>hibernate.cfg.xml</code> file, and run my jsp, I give me </p>
  
  <pre><code>Context initialization failed
  org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in ServletContext resource [/WEB-INF/spring/db.xml]: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: org/hibernate/util/xml/Origin
  </code></pre>
  
  <p>And this is the class I use to test the connection:</p>
  
  <pre><code>    private static final SessionFactory sessionF;
  /*private static final ServiceRegistry serviceR;*/
  
  static {
      try {
          sessionF = new AnnotationConfiguration()
                      .configure().buildSessionFactory();
      }catch(Throwable e) {
          throw new ExceptionInInitializerError(e);
      }
  }
  
  public static Session openSession() throws HibernateException{
      return sessionF.openSession();
  }
  
  public static void closeSession() {
      openSession().close();
  }
  public static void main(String[] args) {
      Session session = HibernateUtil.openSession();
      session.beginTransaction();
  
      int min = 100;
      int max = 101;
  
      long start = System.currentTimeMillis();
      Users aUser = null;
      for(int i = min; i &lt; max; i++) {
          aUser = new Users();
          aUser.setUsername("testusername" + i);
          aUser.setEmail("testemail"+i);
          aUser.setPassword("Hibernate" + i);
          session.save(aUser);
  
          System.out.println(aUser);
      }
      long end = System.currentTimeMillis();
      System.out.println("total: " + (end - start));
      session.getTransaction().commit();
  
      HibernateUtil.closeSession();
  }
  </code></pre>
  
  <p>this is my stack</p>
  
  <pre><code>ERROR: org.springframework.web.context.ContextLoader - Context initialization failed
  org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in ServletContext resource [/WEB-INF/spring/db.xml]: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: org/hibernate/util/xml/Origin
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1420)
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
  at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
  at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
  at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
  at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
  at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:563)
  at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)
  at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)
  at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:276)
  at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:197)
  at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:47)
  at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4973)
  at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5467)
  at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
  at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
  at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
  at java.util.concurrent.FutureTask.run(FutureTask.java:262)
  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
  at java.lang.Thread.run(Thread.java:744)
  Caused by: java.lang.NoClassDefFoundError: org/hibernate/util/xml/Origin
  at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:72)
  at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:225)
  at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:308)
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1477)
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1417)
  ... 21 more
  Caused by: java.lang.ClassNotFoundException: org.hibernate.util.xml.Origin
  at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720)
  at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)
  ... 26 more
  Aug 15, 2014 9:50:49 AM org.apache.catalina.core.StandardContext listenerStart
  SEVERE: Exception sending context initialized event to listener instance of class    org.springframework.web.context.ContextLoaderListener
  org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in ServletContext resource [/WEB-INF/spring/db.xml]: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: org/hibernate/util/xml/Origin
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1420)
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
  at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
  at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
  at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
  at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
  at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:563)
  at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)
  at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)
  at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:276)
  at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:197)
  at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:47)
  at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4973)
  at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5467)
  at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
  at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
  at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
  at java.util.concurrent.FutureTask.run(FutureTask.java:262)
  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
  at java.lang.Thread.run(Thread.java:744)
  Caused by: java.lang.NoClassDefFoundError: org/hibernate/util/xml/Origin
  at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:72)
  at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:225)
  at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:308)
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1477)
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1417)
  ... 21 more
  Caused by: java.lang.ClassNotFoundException: org.hibernate.util.xml.Origin
  at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720)
  at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)
  ... 26 more
  
  Aug 15, 2014 9:50:49 AM org.apache.catalina.core.StandardContext startInternal
  SEVERE: Error listenerStart
  Aug 15, 2014 9:50:49 AM org.apache.catalina.core.StandardContext startInternal
  SEVERE: Context [/example] startup failed due to previous errors
  Aug 15, 2014 9:50:49 AM org.apache.catalina.core.ApplicationContext log
  INFO: Closing Spring root WebApplicationContext
  Aug 15, 2014 9:50:49 AM org.apache.coyote.AbstractProtocol start
  INFO: Starting ProtocolHandler ["http-bio-9183"]
  Aug 15, 2014 9:50:49 AM org.apache.coyote.AbstractProtocol start
  INFO: Starting ProtocolHandler ["ajp-bio-8006"]
  Aug 15, 2014 9:50:49 AM org.apache.catalina.startup.Catalina start
  INFO: Server startup in 2273 ms
  </code></pre>