D
D
Denis2013-12-26 12:29:56
Java
Denis, 2013-12-26 12:29:56

How to solve session creation problem in Hibernate?

I understand what Hibernate is and how it works, a problem arose:
When creating a session by the method:

public class HibernateUtil {
    private static SessionFactory sessionFactory = null;
    private static ServiceRegistry serviceRegistry = null;
    
    static {
        try {
            sessionFactory = new Configuration().configure().buildSessionFactory();
        } catch (Exception e) {
              e.printStackTrace();
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

Everything works fine, however the buildSessionFactory() method is marked as deprecated, it is recommended to use buildSessionFactory(serviceRegistry) instead, but when trying to create a session like this:
public class HibernateUtil {
    private static SessionFactory sessionFactory = null;
    private static ServiceRegistry serviceRegistry = null;
    
    static {
        try {
                //creates the session factory from hibernate.cfg.xml
    		Configuration configuration = new Configuration();
    		configuration.configure();
    		serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).getBootstrapServiceRegistry();        
    		sessionFactory = configuration.buildSessionFactory(serviceRegistry);
        } catch (Exception e) {
              e.printStackTrace();
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

error occurs:
Dec 26, 2013 1:28:48 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager
INFO: HCANN000001: Hibernate Commons Annotations {4.0.4.Final}
Dec 26, 2013 1:28:48 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.3.0.Final}
Dec 26, 2013 1:28:48 PM org.hibernate.cfg.Environment
INFO: HHH000205: Loaded properties from resource hibernate.properties: {hibernate.connection.driver_class=org.h2.Driver, hibernate.service.allow_crawling=false, hibernate.dialect=org.hibernate.dialect.H2Dialect, hibernate.max_fetch_depth=5 , hibernate.format_sql=true, hibernate.generate_statistics=true, hibernate.connection.username=sa, hibernate.connection.url=jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1;MVCC=TRUE, hibernate.bytecode.use_reflection_optimizer= false, hibernate.jdbc.batch_versioned_data=true, hibernate.connection.pool_size=5}
Dec 26, 2013 1:28:48 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Dec 26, 2013 1 :28:48 PM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
Dec 26, 2013 1:28:48 PM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
Dec 26, 2013 1:28:48 PM org.hibernate.internal.util.xml .DTDEntityResolver resolveEntity
WARN: HHH000223: Recognized obsolete hibernate namespace hibernate.sourceforge.net/. Use namespace www.hibernate.org/dtd instead. Refer to Hibernate 3.6 Migration Guide!
Dec 26, 2013 1:28:48 PM org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
java.lang.NullPointerException
at org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1885)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1843)
at Hibernate.Util.HibernateUtil.(HibernateUtil.java:18)
at Hibernate.DAO.Impl.StudentDAOImpl.addStudent(StudentDAOImpl.java:17)
at Main .main(Main.java:22)
What am I doing wrong?
Thanks in advance.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
GChar, 2014-02-19
@GChar

Good afternoon! Struggled with the same issue recently, I have Hibernate 4.3. The following code helped:

public class HibernateUtils {

    private static final SessionFactory sessionFactory = buildSessionFactory();
    private static ServiceRegistry serviceRegistry;

    private static SessionFactory buildSessionFactory() {

        Configuration configuration = new Configuration();
        configuration.configure();
        serviceRegistry = new StandardServiceRegistryBuilder()
                .applySettings(configuration.getProperties()).build();
        return configuration.buildSessionFactory(serviceRegistry);
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}

D
DmitryTM1, 2015-01-13
@DmitryTM1

for some reason I have a red underline serviceRegistry

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question