Test hibernate factory

4

You are giving null exception on line HibernateUtil.getFactory().openSession(); in the main method.

Here are the configuration and test files:

  <?xml version='1.0' encoding='utf-8'?>  
  <!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">  

  <hibernate-configuration>  

    <session-factory>  

        <!-- Database connection settings -->  
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>  
        <property name="connection.url">jdbc:mysql://127.0.0.1:3306/pessoa</property>  
        <property name="connection.username">root</property>  
        <property name="connection.password"></property>  

        <!-- pool de conexão -->  
        <property name="connection.pool_size">1</property>  

        <property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>  

  <property name="current_session_context_class">thread</property>  

  <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>  

        <!-- mostra as querys -->  
        <property name="show_sql">false</property>  

  <property name="hbm2ddl.auto">validate</property>  

        <!-- <mapping resource="org/hibernate/tutorial/domain/Event.hbm.xml" /> -->

    </session-factory>

</hibernate-configuration>

hibernate.cfg.xml:

The connection class (factory):

package util;  

import org.hibernate.SessionFactory;  
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;  
import org.hibernate.cfg.Configuration;  
import org.hibernate.service.ServiceRegistry;  

public class HibernateUtil {  

private static SessionFactory factory;  
private static Configuration configuration;  

public static SessionFactory initialize() {  
try {  
configuration=new Configuration().configure();  

ServiceRegistry registro= new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();  

factory=configuration.buildSessionFactory(registro);  

System.out.println(">> HIBERNATE INICIADO COM SUCESSO.");  
}  

catch (Throwable e) {  
System.out.println(">> FALHA NA INICIAÇÃO DO HIBERNATE.");  
e.printStackTrace();  
}  

return factory;  
}  

public static SessionFactory getFactory() {  
return factory;  
}  

}  

The test class:

package util;  

import org.hibernate.SessionFactory;  

public class HibernateUtilTest {  

public static void main(String args[]) {  
HibernateUtil.getFactory().openSession();  
}  

}  
    
asked by anonymous 09.04.2017 / 03:29

1 answer

2

As you are using HibernateUtil.getFactory () will return null, try to start the factory variable in the getFactory () method.

package util;  

import org.hibernate.SessionFactory;  
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;  
import org.hibernate.cfg.Configuration;  
import org.hibernate.service.ServiceRegistry;  

public class HibernateUtil {  

    private static SessionFactory factory;  
    private static Configuration configuration;  

    public static SessionFactory initialize() {  
        try {  
            configuration=new Configuration().configure();  

            ServiceRegistry registro= new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();  

            factory=configuration.buildSessionFactory(registro);  

            System.out.println(">> HIBERNATE INICIADO COM SUCESSO.");  
        }catch (Throwable e) {  
            System.out.println(">> FALHA NA INICIAÇÃO DO HIBERNATE.");  
            e.printStackTrace();  
        }  

        return factory;  
    }  

    public static SessionFactory getFactory() {  
        id(factory == null)//verificar se a variável já foi instanciada
            initialize();//inicia a variável caso seja null
        return factory;  
    }  

}  
    
14.04.2017 / 23:11