gpt4 book ai didi

java - hibernate.cfg.xml 在源文件中但仍未找到

转载 作者:行者123 更新时间:2023-11-30 02:26:30 24 4
gpt4 key购买 nike

我的 src 文件中有 hibernate.cfg.xml 文件,但仍然收到此错误。请参阅我的 HibernateUtil 类、错误和 xml 文件。我尝试了其他人在其他步骤中发布的大部分解决方案。

这是结构的屏幕截图: enter image description here

import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

public class HiberUtil {

private static final SessionFactory sessionFactory;

static {
try {
// Create the SessionFactory from standard (hibernate.cfg.xml)
// config file.
sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
} catch (Throwable ex) {
// Log the exception.
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
// Configuration configuration = new Configuration().configure();
// StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().
// applySettings(configuration.getProperties());
// sessionFactory = configuration.buildSessionFactory(builder.build());
}

public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}

这是错误:

Aug 06, 2017 10:40:29 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.4.Final}
Aug 06, 2017 10:40:29 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.3.1.Final}
Aug 06, 2017 10:40:29 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Aug 06, 2017 10:40:29 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Aug 06, 2017 10:40:29 PM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
Aug 06, 2017 10:40:29 PM 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 "JavaFX Application Thread" java.lang.ExceptionInInitializerError

如果您需要的话,这里是 hibernate.cfg.xml:

<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/cokolada</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.show_sql">true</property>
<!-- Mapiranje klasa -->
<mapping class="model.Cokolada"/>
</session-factory>
</hibernate-configuration>

最佳答案

首先,了解您从其他线程中尝试过的具体内容会很有帮助,以便能够排除问题并帮助诊断您的问题。

其次,它无法找到您的文件,因为它没有位于正确的位置。 它需要位于您的类路径中。我使用maven,它需要在这里:src/main/资源

Check out this SO post, he gives the best answer here I think.

It is the same for any other time you need a file visible on the classpath. The config file hibernate.cfg.xml needs to be on the classpath. Exactly that and nothing else is key. This can be accomplished in different ways, depending on your project... [follow link and upvote the other guy for the full answer]

关于java - hibernate.cfg.xml 在源文件中但仍未找到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45539119/

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