Testing Factory Hibernate

Asked

Viewed 405 times

4

Is giving null Exception on line HibernateUtil.getFactory().openSession(); in the main method.

Follow how 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();  
}  

}  
  • You have already created the "person" database in Mysql?

  • @Rafael Blum the bank is created, already tested with jdbc, without Maven and worked.

1 answer

2

The way you are using Hibernateutil.getFactory() will return null, try starting 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;  
    }  

}  

Browser other questions tagged

You are not signed in. Login or sign up in order to post.