简体   繁体   中英

New Spring Boot @EntityScan doesn't work

As of Spring ORM v1.4 org.springframework.boot.orm.jpa.EntityScan was deprecated in favor of org.springframework.boot.autoconfigure.domain.EntityScan .

I was going to remove deprecated annotation in favor of new one, but such replacement cause 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]

With org.springframework.boot.orm.jpa.EntityScan annotation, application starts and works correctly.

Here is my config:

@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;
    }
    // ...
  }

It seams that I've missed something, haven't I?

If you take a look at this issue on Spring Boot's issue tracker , you'll find the behaviour changes with the new annotation, as now documented in the release notes .

In your example, the simplest change would be to add a line to call LocalContainerEntityManagerFactoryBean.setPackagesToScan(…​) , as suggested in the release notes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM