Hibernate的两个配置文件的目的是什么? [英] what is the purpose of two config files for Hibernate?

查看:95
本文介绍了Hibernate的两个配置文件的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我目前的项目结构:

  pom.xml 
/ src
/ main
/ resources
hibernate.cfg.xml
/ META-INF
persistence.xml

在这两个文件中都有类似的配置参数( hibernate.cfg.xml persistence.xml ),这看起来很奇怪,但这是我在许多在线示例和教程中看到的。我不明白为什么我需要有两个文件。是否有可能只与一个工作?请解释。



ps。例如,我应该在两个文件中声明 hibernate.dialect ,还是只有一个就足够了?如果是这样,哪一个使用?

解决方案

如果您使用Hibernate的专有API,则需要的hibernate.cfg.xml 。如果您使用的是JPA,即Hibernate EntityManager,则需要 persistence.xml

因此,当您使用 Hibernate专有API或JPA时,您通常不需要这两者。

但是,如果正在使用 Hibernate Proprietary API并且已经有一个 hibernate.cfg.xml (和 hbm.xml XML映射文件),但想要开始使用JPA,可以通过引用 hibernate.cfg.xml 来重用现有的配置文件。在 hibernate.ejb.cfgfile 属性中的 persistence.xml 中 - 并且因此具有这两个文件。重用现有的 hbm.xml 文件是IMO的一个现实场景,可以证明两者保持一致(即使我可能长期迁移到JPA注释)。



引用




This is my current project structure:

pom.xml
/src
  /main
    /resources
      hibernate.cfg.xml
      /META-INF
        persistence.xml

I have very similar configuration params in both files (hibernate.cfg.xml and persistence.xml), which looks strange, but this is what I saw in many online examples and tutorials. I can't understand why do I need to have two files. Is it possible to work just with one? Please explain.

ps. For example, should I declare hibernate.dialect in both files, or just one will be enough? If so, which one to use?

解决方案

If you are using Hibernate's proprietary API, you'll need the hibernate.cfg.xml. If you are using JPA i.e. Hibernate EntityManager, you'll need the persistence.xml.

So you generally don't need both as you use either Hibernate proprietary API or JPA.

However, if you were using Hibernate Proprietary API and already have a hibernate.cfg.xml (and hbm.xml XML mapping files) but want to start using JPA, you can reuse the existing configuration files by referencing the hibernate.cfg.xml in the persistence.xml in the hibernate.ejb.cfgfile property - and thus have both files. Reusing existing hbm.xml files is IMO a realistic scenario that could justify keeping both (even if I'd probably migrate to JPA annotations on the long run).

References

这篇关于Hibernate的两个配置文件的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆