Exception in thread "main" java.lang.ExceptionInInitializerError - Hibernate with IDE Eclipse

1

I can not give in the key with the error. This is my file hibernate.cfg.xml

    <?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>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost/pruebahibernate</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.search.autoregister_listeners">false</property>
    <property name="hibernate.show_sql">true</property>        
    <mapping resource="src/entidades/Contacto.hbm.xml"/>
    </session-factory>
    </hibernate-configuration>

This is HibernateUtil

    public class HibernateUtil
    {
       private static final SessionFactory sessionFactory;   

       static 
       { 
          try 
          { 
           sessionFactory = new Configuration().configure().buildSessionFactory(); 
          } catch (HibernateException he) 
          { 
             System.err.println("Ocurrió un error en la inicialización de la SessionFactory: " + he); 
             throw new ExceptionInInitializerError(he); 
          } 
      }  

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

I have a class Contact with 3 attributes: name, email and phone, which does not implement more than getters and setters. And in summary the class ContactoDAO is this (also implement update, delete, etc.):

    public class ContactoDAO
    {
    private Session sesion; 
    private Transaction tx;  

    public long guardaContacto(Contacto contacto) throws HibernateException 
    { 
      long id = 0;  

      try 
      { 
        iniciaOperacion(); 
        id = (Long) sesion.save(contacto); 
        tx.commit(); 
      } catch (HibernateException he) 
      { 
        manejaExcepcion(he); 
        throw he; 
      } finally 
      { 
        sesion.close(); 
      }  

      return id; 
      }  

    private void iniciaOperacion() throws HibernateException 
    { 
      sesion = HibernateUtil.getSessionFactory().openSession(); 
      tx = sesion.beginTransaction(); 
    } 
    } 

And the mapping file Contacto.hbm.xml is:

    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    <!-- Generated 30/01/2017 23:44:31 by Hibernate Tools 3.5.0.Final -->
    <hibernate-mapping>
    <class name="entidades.Contacto" table="CONTACTO">
      <id name="id" type="long">
        <column name="ID" />
        <generator class="assigned" />
      </id>
      <property name="nombre" type="java.lang.String">
        <column name="NOMBRE" />
      </property>
      <property name="email" type="java.lang.String">
        <column name="EMAIL" />
      </property>
      <property name="telefono" type="java.lang.String">
        <column name="TELEFONO" />
      </property>
    </class>
    </hibernate-mapping>

I get the following error:

Exception in thread "main" java.lang.ExceptionInInitializerError
at entidades.ContactoDAO.iniciaOperacion(ContactoDAO.java:102)
at entidades.ContactoDAO.guardaContacto(ContactoDAO.java:20)
at ejemplo.hibernate.PruebaHibernate.main(PruebaHibernate.java:23)
Caused by: java.lang.NullPointerException
at org.hibernate.cfg.Configuration.addResource(Configuration.java:560)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1587)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1555)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1534)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1508)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1428)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1414)
at entidades.HibernateUtil.<clinit>(HibernateUtil.java:17)

Does anyone see the error at a glance? Thanks!

    
asked by Mai Cai Iney 31.01.2017 в 05:20
source

1 answer

1

The error is that the tx object is null in the context of the guardaContacto method because the iniciaOperacion method is type void , you should modify it by the following:

private Transaction iniciaOperacion() throws HibernateException { 
      sesion = HibernateUtil.getSessionFactory().openSession(); 
      tx = sesion.beginTransaction(); 
      return tx
    } 
    
answered by 31.01.2017 в 12:08