简体   繁体   English

找不到hibernate.cfg.xml

[英]hibernate.cfg.xml not found

I am new to Hibernate, reading this book "Java persistence with Hibernate" and I am trying to implement the example from there. 我是Hibernate的新手,阅读本书“使用Hibernate进行Java持久化”,我试图从那里实现这个例子。 So far my Ant build is successful, but when I try to execute the class containing the main method I am getting this error message: 到目前为止,我的Ant构建是成功的,但是当我尝试执行包含main方法的类时,我收到此错误消息:

19-Nov-2011 18:40:09 org.hibernate.cfg.Environment <clinit>
INFO: Hibernate 3.2.3
19-Nov-2011 18:40:09 org.hibernate.cfg.Environment <clinit>
INFO: hibernate.properties not found
19-Nov-2011 18:40:09 org.hibernate.cfg.Environment buildBytecodeProvider
INFO: Bytecode provider name : cglib
19-Nov-2011 18:40:09 org.hibernate.cfg.Environment <clinit>
INFO: using JDK 1.4 java.sql.Timestamp handling
19-Nov-2011 18:40:09 org.hibernate.cfg.Configuration configure
INFO: configuring from resource: /hibernate.cfg.xml
19-Nov-2011 18:40:09 org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: Configuration resource: /hibernate.cfg.xml
Exception in thread "main" java.lang.ExceptionInInitializerError
    at persistence.HibernateUtil.<clinit>(Unknown Source)
    at hello.Driver.main(Unknown Source)
Caused by: org.hibernate.HibernateException: /hibernate.cfg.xml not found
    at org.hibernate.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:147)
    at org.hibernate.cfg.Configuration.getConfigurationInputStream(Configuration.java:1405)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1427)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1414)
    ... 2 more

It is clear that hibernate can't find my config file, which is located in the root dir. 很明显,hibernate找不到我的配置文件,它位于根目录中。

Project 项目

+lib
<all required libraries>
+src
  +hello
    HelloWorld.java
    Message.java
    message.hbm.xml
  +persistence
    HibernateUtil.java
build.xml
hibernate.cfg.xml

My the complete source code can be found here: http://pastebin.com/bGDUrxUf 我的完整源代码可以在这里找到: http//pastebin.com/bGDUrxUf

I have a running MySQL server with a database hibernateapp and table messages 我有一个运行MySQL服务器与数据库hibernateapp和表消息

Thanks :) 谢谢 :)

The hibernate.cfg.xml file shoul be in root directory of the classpath of your project. hibernate.cfg.xml文件应位于项目类路径的根目录中。 If you using Maven then make sure it should be like src > resources > hibernate.cfg.xml. 如果你使用Maven,那么确保它应该像src> resources> hibernate.cfg.xml。

Your hibernate.cfg.xml needs to be inside the src directory; 你的hibernate.cfg.xml需要在src目录中; otherwise it's not covered by Ant's copymetafiles target, so it won't end up in your compiled classpath. 否则它不会被Ant的copymetafiles目标覆盖,所以它不会在你编译的类路径中结束。

它不应该在您的根目录中,它应该在您的类路径中。

You can load hibernate.cfg.xml from a different directory (not necessarily the classpath) using the configure(File configFile) method that takes the hibernateConfig File argument. 您可以使用带有hibernateConfig File参数的configure(File configFile)方法从其他目录(不一定是类路径)加载hibernate.cfg.xml (note, I am using hibernate 4.3.7) (注意,我正在使用hibernate 4.3.7)

Like this: 像这样:


String hibernatePropsFilePath = "/etc/configs/hibernate.cfg.xml";

File hibernatePropsFile = new File(hibernatePropsFilePath);

Configuration configuration = new Configuration(); 
configuration.configure(hibernatePropsFile);

StandardServiceRegistryBuilder serviceRegistryBuilder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());

ServiceRegistry serviceRegistry = serviceRegistryBuilder.build();

SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);

If you are working on Intellij Idea then make a folder named "resources" under src\\main\\java. 如果您正在使用Intellij Idea那么在src \\ main \\ java下创建一个名为“resources”的文件夹。 Open Module setting of your project, select "Modules" from left and in the "sources" tab select the newly created "resources" folder and mark it as "Resources". 打开项目的模块设置,从左侧选择“模块”,在“源”选项卡中选择新创建的“资源”文件夹并将其标记为“资源”。 Create the hibernate.cfg.xml file inside this newly created "resources" folder. 在这个新创建的“resources”文件夹中创建hibernate.cfg.xml文件。 在此输入图像描述

then this should work 这应该工作

Configuration con = new Configuration().configure("hibernate.cfg.xml");

虽然现在已经很晚了,但解决方案是您需要将此配置文件放在资源文件夹(projectxxxx-> Resources)中,前提是它是一个maven项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM