How to configure Spring without persistence.xml?

后端 未结 4 1872
礼貌的吻别
礼貌的吻别 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:15

    Specify the "packagesToScan" & "persistenceUnitName" properties in the entityManagerFactory bean definition.

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
            <property name="dataSource" ref="dataSource" />
    
            <property name="persistenceUnitName" value="myPersistenceUnit" />
            <property name="packagesToScan" >
                <list>
                    <value>org.mypackage.*.model</value>
                </list>
            </property>
    
            <property name="jpaProperties">
                <props>
                    <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                    <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
                    <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                    <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
                </props>
            </property>
        </bean>
    

    Note that this is for Spring version > 3.1

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-13 05:25

    From Spring Guide Accessing Data with JPA

    @Configuration
    @EnableJpaRepositories
    public class Application {
    
        @Bean
        public DataSource dataSource() {
            return new EmbeddedDatabaseBuilder().setType(H2).build();
        }
    
        @Bean
        public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource, JpaVendorAdapter jpaVendorAdapter) {
            LocalContainerEntityManagerFactoryBean lef = new LocalContainerEntityManagerFactoryBean();
            lef.setDataSource(dataSource);
            lef.setJpaVendorAdapter(jpaVendorAdapter);
            lef.setPackagesToScan("hello");
            return lef;
        }
    
        @Bean
        public JpaVendorAdapter jpaVendorAdapter() {
            HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter();
            hibernateJpaVendorAdapter.setShowSql(false);
            hibernateJpaVendorAdapter.setGenerateDdl(true);
            hibernateJpaVendorAdapter.setDatabase(Database.H2);
            return hibernateJpaVendorAdapter;
        }
    

    Spring Boot

    With Spring Boot enabled application this is even easier:

    Sample application.yaml

    spring:
        datasource:
            url: jdbc:h2:mem:test
            username: sa
            password: sa
            driver-class-name: org.h2.Driver
        jpa:
            database: H2
            show-sql: false
            hibernate:
                format_sql: true
                ddl-auto: auto
    
    0 讨论(0)
  • 2020-12-13 05:37

    MariuszS' answer is good except that the entityManagerFactory method should return EntityManagerFactory. To do that it can be written like this:

    @Bean
    public EntityManagerFactory entityManagerFactory(DataSource dataSource, JpaVendorAdapter jpaVendorAdapter) {
        LocalContainerEntityManagerFactoryBean lef = new LocalContainerEntityManagerFactoryBean();
        lef.setDataSource(dataSource);
        lef.setJpaVendorAdapter(jpaVendorAdapter);
        lef.setPackagesToScan("hello");
        return lef.object();
    }
    

    For future audience: below code worked:

    @Bean (name = "entityManagerFactory")
    public EntityManagerFactory entityManagerFactory(DataSource dataSource, JpaVendorAdapter jpaVendorAdapter)
    {
        LocalContainerEntityManagerFactoryBean lef = new LocalContainerEntityManagerFactoryBean();
        lef.setDataSource(dataSource);
        lef.setJpaVendorAdapter(jpaVendorAdapter);
        lef.setPackagesToScan("*.models*");
        lef.afterPropertiesSet(); // It will initialize EntityManagerFactory object otherwise below will return null
        return lef.getObject();
    }
    
    0 讨论(0)
提交回复
热议问题