如何使用 hibernate.properties 文件而不是 hibernate.cfg.xml

新手上路,请多包涵

我正在尝试使用 Hibernate 连接到 servlet 中的数据库。我读过我们可以使用 hibernate.cfg.xml 或 hibernate.properties 文件来配置会话。对我来说,它使用 xml。现在,当我尝试使用属性而不是 xml 时,它不起作用。它说 找不到 hibernate.cfg.xml 。但我没有提到使用 xml 文件,事实上我已经删除了那个 xml 文件。

请帮我。如果我做错了什么,请纠正我。

原文由 Salman Ahmad 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 402
2 个回答

根据我对休眠的了解,最好的办法是在 hibernate.cfg.xml 文件中定义映射,在 hibernate.properties 中定义其他配置。

另一种配置方法是在名为 hibernate.cfg.xml 的文件中指定完整配置。此文件可用作 hibernate.properties 文件的替代品,或者如果两者都存在,则用于覆盖属性。

hibernate.cfg.xml 一旦你必须调整休眠缓存也更方便。您可以选择使用 hibernate.properties 或 hibernate.cfg.xml 。两者是等价的。

您可以在以下链接中阅读更多相关信息:

http://docs.jboss.org/hibernate/core/3.3/reference/en/html/session-configuration.html

原文由 JoaoFilipeClementeMartins 发布,翻译遵循 CC BY-SA 3.0 许可协议

此代码将默认调用 hibernate.cfg.xml:

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

并且此代码将默认调用 hibernate.properties:

 Configuration configuration = new Configuration();

希望能帮助到你。

原文由 proS 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题