新的Spring Boot @EntityScan不起作用

时间:2017-01-15 17:10:27

标签: java spring spring-boot

截至Spring ORM v1.4 org.springframework.boot.orm.jpa.EntityScan已被弃用,转而使用org.springframework.boot.autoconfigure.domain.EntityScan

我打算删除已弃用的注释以支持新注释,但这样的替换会导致IllegalStateException

Caused by: java.lang.IllegalStateException: No persistence units parsed from {classpath*:META-INF/persistence.xml}
    at org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager.obtainDefaultPersistenceUnitInfo(DefaultPersistenceUnitManager.java:680) ~[spring-orm-4.3.5.RELEASE.jar:4.3.5.RELEASE]

使用org.springframework.boot.orm.jpa.EntityScan注释,应用程序启动并正常工作。

这是我的配置:

@Configuration
@EntityScan("com.app.domain")
@EnableJpaRepositories("com.app.persistence.jpa")
public class JpaInfrastructureConfig {
    // ... config props
    @Bean
    public DataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(driverName);
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        return dataSource;
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
        LocalContainerEntityManagerFactoryBean entityManagerFactory = new LocalContainerEntityManagerFactoryBean();

        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        entityManagerFactory.setJpaVendorAdapter(vendorAdapter);

        entityManagerFactory.setDataSource(dataSource());

        entityManagerFactory.setJpaProperties(new Properties() {{
            put("hibernate.dialect", hibernateDialect);
            put("hibernate.show_sql", hibernateShowSql);
            put("hibernate.hbm2ddl.auto", hibernateHbm2ddl);
        }});

        return entityManagerFactory;
    }

    @Bean
    public JpaTransactionManager transactionManager() {
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
        return transactionManager;
    }
    // ...
  }

接缝我错过了什么,不是吗?

1 个答案:

答案 0 :(得分:0)

如果您查看this issue on Spring Boot's issue tracker,您会发现行为会随着新注释而发生变化,如release notes中所述。

在您的示例中,最简单的更改是添加一行来调用LocalContainerEntityManagerFactoryBean.setPackagesToScan(…​),如发行说明中所述。