找不到resources / hibernate.cfg.xml

时间:2014-06-05 16:47:54

标签: java hibernate configuration persistence

我正在学习在我的Java Web应用程序中使用Hibernate。我有以下sessionFactory开始配置我的hibernate.cfg.xml文件的位置,但它仍然抱怨:resources/hibernate.cfg.xml not found

"资源"文件夹是我的" src"的子目录。 java项目中的文件夹。

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();
    }


}

我在pom.xml中使用的hibernate依赖关系如下所示,但是 intelliJ通过突出显示" Strikethrough"来警告不推荐使用buildSessionFactory()方法

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

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

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

2 个答案:

答案 0 :(得分:1)

找不到您的hibernate配置文件,因为启动webapp时它不在类路径的根目录中...

当您使用Maven时,将hibernate.cfg.xml文件放在src / main / resources文件夹中会导致它自动被复制到WAR文件中的WEB-INF / classes。打开WAR文件,查看hibernate.cfg.xml的放置位置。

如果你的WAR文件中没有hibernate.cfg.xml文件...请仔细检查POM中的maven-war-plugin设置。

关于弃用警告,根据4.3 documentation,不推荐使用buildSessionFactory(),而应该使用buildSessionFactory(ServiceRegistry)。

答案 1 :(得分:1)

谢谢大家的回复。

最后,我已经弄明白了。下面是我在HibernateUtil类中的配置。因为我正在使用Hibernate 4.3.5,所以我已经用最新的方法替换了已弃用的方法。我还将hibernate.cfg.xml移动到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);
        }
    }