首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >找不到cfg.xml资源(/hibernate.cfg.xml)

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

Stack Overflow用户
提问于 2018-01-27 11:22:47
回答 1查看 6.8K关注 0票数 1

所以我有这个main

代码语言:javascript
复制
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中的数据库加载到列表中,然后显示它

和HibernateUtililty类

代码语言:javascript
复制
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文件。

这就是我得到的错误

代码语言:javascript
复制
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

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

EN

回答 1

Stack Overflow用户

发布于 2018-01-27 11:41:36

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

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

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48472270

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档