【问题标题】:Could not locate cfg.xml resource (/hibernate.cfg.xml)找不到 cfg.xml 资源 (/hibernate.cfg.xml)
【发布时间】:2018-01-27 03:22:46
【问题描述】:

所以我有这个主要的

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

为什么会出现这个错误,我该如何解决?

【问题讨论】:

  • 你需要把你的hibernate.cfg.xml放在类路径而不是包里面,或者你需要在configure方法中传递hibernate.cfg.xml的完整路径

标签: java hibernate netbeans-8


【解决方案1】:

你需要把hibernate.cfg.xml放到resources文件夹(\src\main\resources\hibernate.cfg.xml)

参考以下问题的答案: Location of hibernate.cfg.xml in project?

【讨论】:

  • 我已经通过将它直接放入 /src 来修复它,但现在我有另一个问题,它没有显示表中的数据,而是显示了一堆 [Ljava.lang.Object;@4a8b5227有不同的数字
  • 你的表“pagos”包含什么样的数据?请举例说明。另外,您正在对对象调用 toString()。这就是为什么它打印默认格式“Ljava.lang.Object .....”请参考以下问题的答案以使用从数据库中获取数据休眠:stackoverflow.com/questions/20781286/…
  • 表中所有数据都是Strings
  • 请参考以下问题的答案以使用hibernate从数据库中获取数据:stackoverflow.com/questions/20781286/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-06-11
  • 2016-06-14
  • 1970-01-01
  • 2021-07-18
  • 1970-01-01
  • 2014-07-26
  • 1970-01-01
相关资源
最近更新 更多