How to configure Spring without persistence.xml?

后端 未结 4 1874
礼貌的吻别
礼貌的吻别 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条回答
  •  -上瘾入骨i
    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();
    }
    

提交回复
热议问题