gpt4 book ai didi

java - Hibernate 找不到 hibernate.cfg.xml

转载 作者:行者123 更新时间:2023-11-30 03:44:35 25 4
gpt4 key购买 nike

我用gradle做了一个java项目,很简单,它是一个任务管理器,我有一个Person类和一个Task类,我想使用hibernate,但是,hibernate不加载hibernate.cfg.xml ,我尝试了很多解决方案,但一切都有效。

我有一个HibernateUtil.java像这样写:

package org.gradle.util;

import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {

private static final SessionFactory sessionFactory = buildSessionFactory();

private static SessionFactory buildSessionFactory() {
try {
// Create the SessionFactory from hibernate.cfg.xml
return new Configuration().configure().buildSessionFactory(
new StandardServiceRegistryBuilder().build() );
}
catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}

public static SessionFactory getSessionFactory() {
return sessionFactory;
}

}

这是布局:

/my-project
/src
/main
/java
/org
/gradle
Main.java
/controllers
PersonController.java
TaskController.java
/domains
Person.java
Task.java
Person.hbm.xml
Task.hbm.xml
/util
HibernateUtil.java
/java
/resources
hibernate.cfg.xml
/org
/gradle
/test
/java
/org
/gradle
PersonTest.java
TaskTest.java

这是完整的跟踪:

Sep 26, 2014 11:32:11 AM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.5.Final}
Sep 26, 2014 11:32:11 AM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.3.6.Final}
Sep 26, 2014 11:32:11 AM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Sep 26, 2014 11:32:11 AM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Sep 26, 2014 11:32:11 AM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
Sep 26, 2014 11:32:11 AM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
Initial SessionFactory creation failed.org.hibernate.HibernateException: /hibernate.cfg.xml not found
Exception in thread "main" java.lang.ExceptionInInitializerError
at org.gradle.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:20)
at org.gradle.util.HibernateUtil.<clinit>(HibernateUtil.java:9)
at org.gradle.controllers.PersonController.add(PersonController.java:10)
at org.gradle.Main.main(Main.java:12)
Caused by: org.hibernate.HibernateException: /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 org.hibernate.cfg.Configuration.configure(Configuration.java:2054)
at org.gradle.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:14)
... 3 more

我希望你们能帮助我,这让我很伤心。如果您需要更多内容,请告诉我。

最佳答案

根据this Hibernate 4+Gradle 教程:

您的resources文件夹应位于main下,而不是java下:

编辑:

可能,您在构建 session 工厂的方式中遗漏了一些东西:

Configuration configuration = new Configuration().configure();
StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
sessionFactory = configuration.buildSessionFactory(builder.build());

而不是:

return new Configuration().configure().buildSessionFactory(
new StandardServiceRegistryBuilder().build() );

您必须应用设置才能将配置的属性传递给构建器:

关于java - Hibernate 找不到 hibernate.cfg.xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26062057/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com