title: Configure sessionFactory in hibernate in standalone application
body: <p>i am trying to make a 3rd party simple standalone/swing application that uses hibernate to connect on database for another application, so here's what i did:</p>
  
  <p><strong>1- Jars used:</strong></p>
  
  <pre><code>hibernate-core-3.5.1-Final
  hibernate-entitymanager-3.5.1-Final
  hibernate-jpa-2.0-api-1.0.0.Final
  hibernate-annotations-3.5.1-Final
  hibernate-commons-annotations-3.2.0.Final
  dom4j-1.6.1
  slf4j-api-1.6.4
  slf4j-log4j12-1.6.4
  log4j-1.2.16.jar
  commons-collections-3.2
  jta-1.1
  mysql-connector-java-5.1.14 (or compatible connector with your DB)
  commons-logging-1.1.1
  commons-collections-3.2
  </code></pre>
  
  <p><strong>2- hibernate.cfg.xml (it's inside the src folder):</strong></p>
  
  <pre><code>&lt;?xml version='1.0' encoding='utf-8'?&gt;
  &lt;!DOCTYPE hibernate-configuration PUBLIC
  "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"&gt;
  &lt;hibernate-configuration&gt;
     &lt;session-factory&gt;
  
              &lt;property name="show_sql"&gt;true&lt;/property&gt;
              &lt;property name="format_sql"&gt;true&lt;/property&gt;
              &lt;property name="connection.driver_class"&gt;com.mysql.jdbc.Driver&lt;/property&gt;
              &lt;property name="connection.url"&gt;jdbc:mysql://127.0.0.1:3306/myapp&lt;/property&gt;
              &lt;property name="connection.username"&gt;myuser&lt;/property&gt;
              &lt;property name="connection.password"&gt;mypass&lt;/property&gt;
              &lt;property name="dialect"&gt;org.hibernate.dialect.MySQLDialect&lt;/property&gt;
              &lt;property name="current_session_context_class"&gt;thread&lt;/property&gt;
  
     &lt;/session-factory&gt;
  &lt;/hibernate-configuration&gt;
  </code></pre>
  
  <p><strong>3- SessionFactoryHelper:</strong></p>
  
  <pre><code>public class SessionFactoryHelper {
  
      private static final SessionFactory sessionFactory;
  
      static {
          try {
              /*
               * Build a SessionFactory object from session-factory configuration
               * defined in the hibernate.cfg.xml file. In this file we register
               * the JDBC connection information, connection pool, the hibernate
               * dialect that we used and the mapping to our hbm.xml file for each
               * POJO (Plain Old Java Object).
               */
              sessionFactory = new Configuration().configure()
                      .buildSessionFactory();
          } catch (Throwable e) {
              System.err.println("Error in creating SessionFactory object."
                      + e.getMessage());
              throw new ExceptionInInitializerError(e);
          }
      }
  
      /*
       * A static method for other application to get SessionFactory object
       * initialized in this helper class.
       */
      public static SessionFactory getSessionFactory() {
          return sessionFactory;
      }
  }
  </code></pre>
  
  <p><strong>4- Sample Query:</strong></p>
  
  <pre><code>Session session = SessionFactoryHelper.getSessionFactory()
              .getCurrentSession();
      session.beginTransaction();
      int count = (Integer) session.createSQLQuery(
              "select count(*) from users").uniqueResult();
      session.getTransaction().commit();
      System.out.println("Users Count: " + count);
  </code></pre>
  
  <p>when running the application, i gets the following exception:</p>
  
  <pre><code>Error in creating SessionFactory object.invalid configuration
  Exception in thread "main" java.lang.ExceptionInInitializerError
      at com.xeno.xecamp.desktopManagement.SessionFactoryHelper.&lt;clinit&gt;(SessionFactoryHelper.java:24)
      at com.myapp.Main.main(Main.java:9)
  Caused by: org.hibernate.MappingException: invalid configuration
      at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1579)
      at org.hibernate.cfg.Configuration.configure(Configuration.java:1520)
      at org.hibernate.cfg.Configuration.configure(Configuration.java:1506)
      at com.xeno.xecamp.desktopManagement.SessionFactoryHelper.&lt;clinit&gt;(SessionFactoryHelper.java:19)
      ... 1 more
  Caused by: org.xml.sax.SAXParseException: Document is invalid: no grammar found.
      at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:195)
      at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:131)
      at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:384)
      at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:318)
      at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:250)
      at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl$NSContentDriver.scanRootElementHook(XMLNSDocumentScannerImpl.java:626)
      at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:3103)
      at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:922)
      at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:648)
      at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:140)
      at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:511)
      at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:808)
      at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
      at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:119)
      at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1205)
      at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:522)
      at org.dom4j.io.SAXReader.read(SAXReader.java:465)
      at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1576)
      ... 4 more
  </code></pre>
  
  <p>can anyone please tell me what's wrong with my configuration ?</p>