Hibernate的两个配置文件的目的是什么?

时间:2021-12-24 23:58:13

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.

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

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

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

2 个解决方案

#1


58  

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.

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

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

因此,当您使用Hibernate专有API或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).

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

References

  • Hibernate EntityManager
  • Hibernate EntityManager 2.2.2。引导

#2


15  

hibernate.cfg.xml is for Hibernate; persistence.xml is for JPA.

hibernate.cfg.xml用于Hibernate; persistence.xml适用于JPA。

If you do Hibernate without JPA, you don't need the latter.

如果你在没有JPA的情况下进行Hibernate,则不需要后者。

If you do JPA, you have to have a provider implementation, which means Hibernate, EclipseLink, etc. (There may be other legit JPA implementations, but I don't have time to check right now.)

如果你做JPA,你必须有一个提供者实现,这意味着Hibernate,EclipseLink等。(可能有其他合法的JPA实现,但我现在没时间检查。)

#1


58  

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.

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

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

因此,当您使用Hibernate专有API或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).

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

References

  • Hibernate EntityManager
  • Hibernate EntityManager 2.2.2。引导

#2


15  

hibernate.cfg.xml is for Hibernate; persistence.xml is for JPA.

hibernate.cfg.xml用于Hibernate; persistence.xml适用于JPA。

If you do Hibernate without JPA, you don't need the latter.

如果你在没有JPA的情况下进行Hibernate,则不需要后者。

If you do JPA, you have to have a provider implementation, which means Hibernate, EclipseLink, etc. (There may be other legit JPA implementations, but I don't have time to check right now.)

如果你做JPA,你必须有一个提供者实现,这意味着Hibernate,EclipseLink等。(可能有其他合法的JPA实现,但我现在没时间检查。)