How to configure Spring without persistence.xml?

后端 未结 4 1875
礼貌的吻别
礼貌的吻别 2020-12-13 05:07

I\'m trying to set up spring xml configuration without having to create a futher persistence.xml. But I\'m constantly getting the following exception, even thou

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-13 05:22

    Assuming that you have a PersistenceProvider implementation (e.g. org.hibernate.jpa.HibernatePersistenceProvider), 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.

提交回复
热议问题