JPA without persistence.xml

后端 未结 3 1233
无人共我
无人共我 2021-01-03 11:40

I\'m trying to get started with using Guice Persist and JPA, which recommends using configuration via persistence.xml. Coming from a native Hibernate background where confi

3条回答
  •  时光说笑
    2021-01-03 12:16

    Assuming that you have a PersistenceProvider implementation (e.g. Hibernate), you can use the PersistenceProvider#createContainerEntityManagerFactory(PersistenceUnitInfo info, Map map) method to bootstrap an EntityManagerFactory without needing a persistence.xml.

    However, it's annoying that you have to implement the PersistenceUnitInfo interface, so you are better off using Spring or Hibernate which both support bootstrapping JPA without a persistence.xml file:

    this.nativeEntityManagerFactory = provider.createContainerEntityManagerFactory(
        this.persistenceUnitInfo, 
        getJpaPropertyMap()
    );
    

    Where the PersistenceUnitInfo is implemented by the Spring-specific MutablePersistenceUnitInfo class.

    Check out this article for a nice demonstration of how you can achieve this goal with Hibernate.

提交回复
热议问题