gpt4 book ai didi

java - 无法加载 hibernate.cfg.xml

转载 作者:行者123 更新时间:2023-12-01 18:14:29 27 4
gpt4 key购买 nike

我正在学习 hibernate 教程并遇到了这个问题。这是我的代码

public class HibernateTest {
public static void main(String[] args) {
System.out.println(args[0]);
User user = new User();
user.setId(1);
user.setName("John Smith");
SessionFactory sessionFactory = createSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();
session.save(user);
session.getTransaction().commit();
}

public static SessionFactory createSessionFactory() {
Configuration configuration = new Configuration();
configuration.configure();
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(
configuration.getProperties()).build();
return configuration.buildSessionFactory(serviceRegistry);
}
}

这是我的项目的结构。

enter image description here

但是我遇到了一个异常(exception),说

Exception in thread "main" 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:2095)
at org.hibernate.cfg.Configuration.configure(Configuration.java:2076)
at org.hibernate.cfg.Configuration.configure(Configuration.java:2056)
at HibernateTest.createSessionFactory(HibernateTest.java:25)
at HibernateTest.main(HibernateTest.java:16)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)

最佳答案

将文件移动到 src/main/resources 中,在构建时它将被复制到类路径中

关于java - 无法加载 hibernate.cfg.xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30508702/

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