What is the purpose of the two configuration files for Hibernate?

This is my current project structure:

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

I have very similar configuration parameters 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 cannot understand why I need to have two files. Is it possible to work with one? Please explain.

ps. For example, should hibernate.dialect be declared in both files, or is one enough? If so, which one to use?

+43
java maven-2 hibernate
Sep 27 '10 at 20:18
source share
2 answers

If you use the Hibernate API, you will need hibernate.cfg.xml . If you are using JPA ie Hibernate EntityManager, you will need persistence.xml .

Thus, you do not need both at all, since you are using either the Hibernate API or JPA.

However, if you used the Hibernate Proprietary API and already have the hibernate.cfg.xml (and hbm.xml XML) hbm.xml files, but want to start using JPA, you can reuse existing configuration files by referencing hibernate.cfg.xml in persistence.xml in the hibernate.ejb.cfgfile property - and therefore has both files. Reusing existing hbm.xml files is an IMO realistic scenario that can justify keeping both (even if I probably go to JPA annotations in the end).

References

+53
Sep 27 '10 at 22:34
source share

hibernate.cfg.xml for sleep mode; persistence.xml for JPA.

If you are doing Hibernate without a JPA, you do not need the latter.

If you are doing JPA, you must have a provider implementation, which means Hibernate, EclipseLink, etc. (There may be other legitimate JPA implementations, but I don’t have time to check now.)

+15
Sep 27 2018-10-0920:
source share



All Articles