所以我有这个主要的
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();
}
}
}
我只想从MySQL中的数据库中将表加载到List中,然后显示它
和HibernateUtililty类
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;
}
}
所有内容都在同一个包中,包含hibernate.reveng.xml,hibernate.cfg.xml以及tables.java和hbm.xml文件。
这是我得到的错误
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
为什么它会给我这个错误,我该如何解决?
答案 0 :(得分:1)
您需要将hibernate.cfg.xml放在resources文件夹(\ src \ main \ resources \ hibernate.cfg.xml)中
请参阅以下问题的答案: Location of hibernate.cfg.xml in project?