Hibernate 的两个配置文件的目的是什么? [英] what is the purpose of two config files for Hibernate?
问题描述
这是我目前的项目结构:
This is my current project structure:
pom.xml
/src
/main
/resources
hibernate.cfg.xml
/META-INF
persistence.xml
我在两个文件(hibernate.cfg.xml
和 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.例如,我应该在两个文件中都声明 hibernate.dialect
还是一个就足够了?如果是这样,使用哪个?
ps. For example, should I declare hibernate.dialect
in both files, or just one will be enough? If so, which one to use?
推荐答案
如果您正在使用 Hibernate 的专有 API,您将需要 hibernate.cfg.xml
.如果您使用 JPA,即 Hibernate EntityManager,您将需要 persistence.xml
.
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 或 JPA 时,通常不需要两者.
So you generally don't need both as you use either Hibernate proprietary API or JPA.
但是,如果您正在使用 Hibernate 专有 API 并且已经有一个 hibernate.cfg.xml
(和 hbm.xml
XML 映射文件) 但是想开始使用JPA,可以通过引用hibernate.ejb中的
属性 - 因此有两个文件.重用现有的 persistence.xml
中的hibernate.cfg.xml
来重用现有的配置文件.cfgfilehbm.xml
文件是 IMO 一个现实的场景,可以证明同时保留两者(即使从长远来看我可能会迁移到 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 的两个配置文件的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!