1

So I have this main

package Hibernate;


import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

public class Hibernate {

/**
 * @param args the command line arguments
 */
private static SessionFactory sessionFactory = null;
public static void main(String[] args) {
    // TODO code application logic here
     Session session = null;
    try {
        try {
            sessionFactory = UtilHibernate.getSessionFactory();
            session = sessionFactory.openSession();

            List listapagos;
            listapagos = session.createNativeQuery("SELECT * FROM pagos").list();

            for (Object pagos : listapagos)
                System.out.println(pagos.toString());

            System.out.println("Finalizado.");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    } finally {

        session.close();
    }
}
}

Where I just want to load a table into the List from a database in MySQL and then show it

And the HibernateUtililty class

import org.hibernate.cfg.Configuration;
import org.hibernate.SessionFactory;
import org.hibernate.*;
import org.hibernate.service.ServiceRegistry;


public class UtilHibernate {

public static final SessionFactory sessionFactory;


static {
    try {
        // Create the SessionFactory from standard (hibernate.cfg.xml) 
        // config file.
        sessionFactory = new Configuration().configure().buildSessionFactory();
    } catch (Throwable ex) {
        // Log the exception. 
        System.err.println("Initial SessionFactory creation failed." + ex);
        throw new ExceptionInInitializerError(ex);
    }

}

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

Everything is in the same package, with hibernate.reveng.xml,hibernate.cfg.xml and the tables.java and hbm.xml files.

This is the error I'm getting

INFO: HHH000206: hibernate.properties not found
Initial SessionFactory creation failed.org.hibernate.internal.util.config.ConfigurationException: Could not locate cfg.xml resource [hibernate.cfg.xml]
Exception in thread "main" java.lang.NullPointerException
at hibernate.Hibernate.main(Hibernate.java:42)
C:\Users\usuario\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53:   Java returned: 1

Why is it giving me that error and how do I fix it?

  • 1
    you need to put your hibernate.cfg.xml in class path instead of inside package, or you need pass full path of hibernate.cfg.xml in configure method – Bhushan Uniyal Jan 27 '18 at 04:04

1 Answers1

4

You need to place hibernate.cfg.xml in resources folder(\src\main\resources\hibernate.cfg.xml)

Refer the answer to the following question: Location of hibernate.cfg.xml in project?

Gaurav
  • 296
  • 1
  • 6
  • 21
  • I already fixed it by putting it directly into /src but now I have another issue, Its not showing the data in the table, instead its showing a bunch of [Ljava.lang.Object;@4a8b5227 with different numbers – EndymionSpring Jan 28 '18 at 05:43
  • What kind of data your table "pagos" contains? please state an example.Also, you are calling toString() on the objects.That's why its printing the default format "Ljava.lang.Object....." Please refer the answers to the following question to fetch data from database using hibernate: https://stackoverflow.com/questions/20781286/how-to-fetch-data-from-database-in-hibernate?rq=1 – Gaurav Jan 29 '18 at 09:20
  • All the data in the table are Strings – EndymionSpring Jan 30 '18 at 03:03
  • Please refer the answers to the following question to fetch data from database using hibernate: https://stackoverflow.com/questions/20781286/how-to-fetch-data-from-database-in-hibernate – Gaurav Jan 30 '18 at 06:54