1 Reply Latest reply on Sep 1, 2011 8:42 AM by smarlow

    how to create a sessionFactory without hibernate.cfg.xml in as7

    jedierikb

      In Hibernate 3/JBoss4, I had a hibernate.cfg.xml with information about how to connect to my remote database.

       

      Question 1:

      In this file was information like the connection url, dialect and mapping information for persisted classes (e.g. <mapping class="User"/>.  Now it seems that the url, dialect are configured using stanalone.xml.  But how do I tell JBoss/Hibernate about the mapped classes?

       

      Question 2:

      I was able to use hibernate.cfg.xml to open sessions using a SessionFactory like this:

       

      import org.hibernate.SessionFactory;

      import org.hibernate.cfg.Configuration;

       

       

      public class HibernateUtil {

       

       

          private static final SessionFactory sessionFactory = buildSessionFactory();

       

       

          private static SessionFactory buildSessionFactory() {

              try {

                  // Create the SessionFactory from hibernate.cfg.xml

                  SessionFactory f = new Configuration().configure().buildSessionFactory();

                  return f;

              }

              catch (Throwable ex) {

                  // Make sure you log the exception, as it might be swallowed

                  System.err.println("Initial SessionFactory creation failed." + ex);

                  throw new ExceptionInInitializerError(ex);

              }

          }

       

       

          public static SessionFactory getSessionFactory() {

              return sessionFactory;

          }

       

       

      But now, in Hibernate 4, import org.hibernate.cfg.Configuration is deprecated.

       

      How do I instantiate a SessionFactory now?