what is the purpose of two config files for Hibernate?

前端 未结 2 880
醉酒成梦
醉酒成梦 2020-12-02 06:35

This is my current project structure:

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

I

2条回答
  •  温柔的废话
    2020-12-02 07:15

    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

提交回复
热议问题