Java 找不到cfg.xml资源(/hibernate.cfg.xml)

Java 找不到cfg.xml资源(/hibernate.cfg.xml),java,hibernate,netbeans-8,Java,Hibernate,Netbeans 8,所以我有一个主要的 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;

所以我有一个主要的

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中的数据库将一个表加载到列表中,然后显示它

还有冬眠班

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

为什么它会给我这个错误?我如何修复它?

您需要将hibernate.cfg.xml放在resources文件夹(\src\main\resources\hibernate.cfg.xml)中

请参阅以下问题的答案:

您需要将hibernate.cfg.xml放入资源文件夹(\src\main\resources\hibernate.cfg.xml)

请参阅以下问题的答案:

您需要将hibernate.cfg.xml放在类路径而不是包内,或者您需要在配置方法中传递hibernate.cfg.xml的完整路径您需要将hibernate.cfg.xml放在类路径而不是包内,或者你需要在configure method中传递hibernate.cfg.xml的完整路径。我已经通过将其直接放入/src中修复了它,但现在我遇到了另一个问题,它没有在表中显示数据,而是显示了一组具有不同编号的[Ljava.lang.Object;@4a8b5227您的表“pagos”中的数据类型包含?请说明一个示例。另外,您正在对对象调用toString()。这就是为什么它会打印默认格式“Ljava.lang.Object…”请参考以下问题的答案以使用hibernate从数据库中获取数据:表中的所有数据都是字符串请参考以下问题的答案以使用hibernate从数据库中获取数据:我已经通过将其直接放入/src进行了修复,但现在我遇到了另一个问题,它没有在表中显示数据,而是显示一组不同编号的[Ljava.lang.Object;@4a8b5227您的表“pagos”包含什么类型的数据?请说明一个示例。此外,您正在对对象调用toString()。这就是为什么它打印默认格式“Ljava.lang.Object…”使用hibernate从数据库获取数据,请参考以下问题的答案:表中的所有数据都是字符串请参考以下问题的答案,使用hibernate从数据库获取数据: