找不到资源/ hibernate.cfg.xml [英] resources/hibernate.cfg.xml not found

查看:168
本文介绍了找不到资源/ hibernate.cfg.xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习在我的Java web应用程序中使用Hibernate。我有以下sessionFactory开始与我的hibernate.cfg.xml文件的位置配置,但它仍然抱怨:资源/ hibernate.cfg.xml找不到

I am learning to use Hibernate in my Java web app. I have the following sessionFactory started with a configuration for the location of my hibernate.cfg.xml file, but it still complains: resources/hibernate.cfg.xml not found

resources文件夹是java项目中src文件夹的子目录。

public class HibernateUtil
{
    private static final SessionFactory sessionFactory = new Configuration().configure("resources/hibernate.cfg.xml").buildSessionFactory();

    public static void main(String[] args)
    {
        Session session = sessionFactory.getCurrentSession();
        Transaction tx = session.beginTransaction();

        Book book1 = new Book("John Wright", "Unknown Title");
        session.save(book1);
        tx.commit();

        System.out.println("Book committed: book title: " + book1.getTitle() + ", author is: " + book1.getAuthor());
        sessionFactory.close();
    }


}



hibernate依赖我在我的pom.xml中使用如下,但 intelliJ通过突出显示删除线来警告buildSessionFactory()方法已被废弃

The hibernate dependency I'm using in my pom.xml is as follows, but intelliJ warns that the buildSessionFactory() method is deprecated by highlight a "Strikethrough"

<dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.3.5.Final</version>
        </dependency>

我在最底层发布我的错误消息。出了什么问题?谢谢

I am posting the error message from my i at the very bottom as well. What is going wrong? thanks

10:43:12.737 [main] INFO  o.h.annotations.common.Version - HCANN000001: Hibernate Commons Annotations {4.0.4.Final}
10:43:12.746 [main] INFO  org.hibernate.Version - HHH000412: Hibernate Core {4.3.1.Final}
10:43:12.747 [main] INFO  org.hibernate.cfg.Environment - HHH000206: hibernate.properties not found
10:43:12.749 [main] INFO  org.hibernate.cfg.Environment - HHH000021: Bytecode provider name : javassist
Exception in thread "main" java.lang.ExceptionInInitializerError
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:259)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:116)
Caused by: org.hibernate.HibernateException: resources/hibernate.cfg.xml not found
    at org.hibernate.internal.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:173)
    at org.hibernate.cfg.Configuration.getConfigurationInputStream(Configuration.java:2093)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:2074)
    at persistence.HibernateUtil.<clinit>(HibernateUtil.java:14)
    ... 3 more
10:43:12.763 [main] INFO  org.hibernate.cfg.Configuration - HHH000043: Configuring from resource: resources/hibernate.cfg.xml
10:43:12.763 [main] INFO  org.hibernate.cfg.Configuration - HHH000040: Configuration resource: resources/hibernate.cfg.xml

Process finished with exit code 1


推荐答案

谢谢大家的回复。

最后,我明白了。以下是我的HibernateUtil类中的配置。因为我使用的是Hibernate 4.3.5,所以我用最新的方法替换了弃用的方法。此外,我将我的hibernate.cfg.xml移至 src / main / java / resources

Finally, I've figured it out. Below is my configuration in my HibernateUtil class. Because I'm using Hibernate 4.3.5, I've replaced the deprecated methods with the latest ones. Also I moved my hibernate.cfg.xml to src/main/java/resources.

static {
        try {
            Configuration configuration = new Configuration().configure("/resources/hibernate.cfg.xml");
            StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
            sessionFactory = configuration.buildSessionFactory(builder.build());
        } catch (HibernateException ex) {
            System.err.println("Initial sessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

这篇关于找不到资源/ hibernate.cfg.xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆