java Web 应用程序 - 在哪里放置 hibernate.cfg.xml 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14142631/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 15:13:36  来源:igfitidea点击:

web application - where to place hibernate.cfg.xml file?

javahibernatehibernate.cfg.xml

提问by Quak

I am writing a web application and I have to add hibernate. I configured maven (pom.xml) etc. but now I am getting the following error:

我正在编写一个 Web 应用程序,我必须添加休眠。我配置了 maven (pom.xml) 等,但现在出现以下错误:

exception
javax.servlet.ServletException: org.hibernate.HibernateException: /hibernate.cfg.xml not found

I am using NetBeans. I tried moving this file to WEB-INF, root project folder, src directory (default package) but it's still not working. How can I do? I don't want to set path to this file programmatically like this:

我正在使用 NetBeans。我尝试将此文件移动到 WEB-INF、根项目文件夹、src 目录(默认包),但它仍然无法正常工作。我能怎么做?我不想像这样以编程方式设置此文件的路径:

Configuration cfg = new Configuration();
cfg.addResource("/some/path/to/this/file/Hibernate.cfg.xml");

采纳答案by Adam Sznajder

I always put it into WEB-INF/classesdirectory (compiled files are stored there).

我总是把它放在WEB-INF/classes目录中(编译后的文件存储在那里)。

回答by Anurag Kapur

You need to add the hibernate.cfg.xml to a folder in the classpath. In a webb app, WEB-INF/classes is in the classpath by default. You can either use that folder or create a new one for your resources (assuming you want to keep them separate) and then set the new folder in classpath by adjusting your project settings.

您需要将 hibernate.cfg.xml 添加到类路径中的文件夹中。在 webb 应用程序中,WEB-INF/classes 默认在类路径中。您可以使用该文件夹或为您的资源创建一个新文件夹(假设您希望将它们分开),然后通过调整项目设置在类路径中设置新文件夹。

回答by Arthur

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. (note, am using hibernate 4.3.7)

您可以使用带有 hibernateConfig File 参数的 configure(File configFile) 方法从不同的目录(不一定是类路径)加载 hibernate.cfg.xml。(注意,我使用的是休眠 4.3.7)

The advantage is, you can place your hibernate configs file in a separate directory which you are bound to have access to (for maintenance or change purposes) other than bundling it together with the .war file which you may not have access to.

优点是,您可以将您的休眠配置文件放在一个单独的目录中,您必须有权访问该目录(用于维护或更改目的),而不是将它与您可能无法访问的 .war 文件捆绑在一起。

Example follows:

示例如下:



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


回答by Krishna

This file must be in the root of classpath of the application. That is under WEB-INF/classes

此文件必须位于应用程序的类路径的根目录中。那是在 WEB-INF/classes 下