Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 

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?

like image 987
yegor256 Avatar asked Sep 27 '10 20:09

yegor256


People also ask

What are the 2 configuration files in hibernate?

There is the two way of mapping in hibernate – The first one which is by using the hibernate annotation and the second one which is by using the hbm. xml.

What is the use of configuration file in hibernate?

As Hibernate can operate in different environments, it requires a wide range of configuration parameters. These configurations contain the mapping information that provides different functionalities to Java classes. Generally, we provide database related mappings in the configuration file.

Can we have multiple configuration files in hibernate?

You can load hibernate configuration files programatically.

In which file do you provide the configuration for the hibernate?

Hibernate also requires a set of configuration settings related to database and other related parameters. All such information is usually supplied as a standard Java properties file called hibernate. properties, or as an XML file named hibernate. cfg.


1 Answers

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 EntityManager
    • 2.2.2. Bootstrapping
like image 180
Pascal Thivent Avatar answered Oct 04 '22 05:10

Pascal Thivent