Reputation: 11
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?
Upvotes: 1
Views: 8392
Reputation: 406
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?
Upvotes: 4