Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify packagesToScan in HibernateJpaAutoConfiguration?

I'm using HibernateJpaAutoConfiguration directly in a Spring unit test. While Hibernate and EntityManager is configured, no entities are scanned.

Exception

10:29:36.377 [main] INFO  o.s.b.f.a.AutowiredAnnotationBeanPostProcessor - JSR-330 javax.inject.Inject' annotation found and supported for autowiring 10:29:36.505 [main] TRACE o.s.b.b.PropertiesConfigurationFactory - Property Sources: org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor$FlatPropertySources@65f8f5ae 10:29:36.638 [main] TRACE o.s.b.b.PropertiesConfigurationFactory - Property Sources: org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor$FlatPropertySources@65f8f5ae 10:29:36.716 [main] TRACE o.s.b.b.PropertiesConfigurationFactory - Property Sources: org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor$FlatPropertySources@65f8f5ae 10:29:36.818 [main] INFO  o.s.o.j.LocalContainerEntityManagerFactoryBean - Building JPA container EntityManagerFactory for persistence unit 'default' 10:29:36.842 [main] INFO  o.h.jpa.internal.util.LogHelper - HHH000204: Processing PersistenceUnitInfo [     name: default     ...] 10:29:36.979 [main] INFO  org.hibernate.Version - HHH000412: Hibernate Core {4.3.6.Final} 10:29:36.980 [main] INFO  org.hibernate.cfg.Environment - HHH000206: hibernate.properties not found 10:29:36.982 [main] INFO  org.hibernate.cfg.Environment - HHH000021: Bytecode provider name : javassist 10:29:37.234 [main] INFO  o.h.annotations.common.Version - HCANN000001: Hibernate Commons Annotations {4.0.4.Final} 10:29:37.599 [main] INFO  org.hibernate.dialect.Dialect - HHH000400: Using dialect: org.hibernate.dialect.PostgreSQL9Dialect 10:29:37.608 [main] INFO  o.h.e.j.internal.LobCreatorBuilder - HHH000424: Disabling contextual LOB creation as createClob() method threw error : java.lang.reflect.InvocationTargetException 10:29:37.648 [main] INFO  o.h.h.i.a.ASTQueryTranslatorFactory - HHH000397: Using ASTQueryTranslatorFactory 10:29:37.742 [main] INFO  o.h.tool.hbm2ddl.SchemaUpdate - HHH000228: Running hbm2ddl schema update 10:29:37.742 [main] INFO  o.h.tool.hbm2ddl.SchemaUpdate - HHH000102: Fetching database metadata 10:29:37.744 [main] INFO  o.h.tool.hbm2ddl.SchemaUpdate - HHH000396: Updating schema 10:29:37.745 [main] INFO  o.h.tool.hbm2ddl.SchemaUpdate - HHH000232: Schema update complete 

My workaround is to create my own LocalContainerEntityManagerFactoryBean as follows:

    final LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();     factoryBean.setPersistenceUnitName("buzzPU"); // persistence.xml     factoryBean.setDataSource(dataSource);     factoryBean.setJpaVendorAdapter(jpaVendorAdapter);     factoryBean.setPersistenceXmlLocation("classpath*:META-INF/donotparsepersistence.xml");     factoryBean.setPackagesToScan("org.soluvas.buzz.core.jpa"); 

Note that I don't use META-INF/persistence.xml

like image 745
Hendy Irawan Avatar asked Aug 18 '14 15:08

Hendy Irawan


People also ask

Which of the following values can be used to configure a BootstrapMode with JPA considering JPA 2.1 or above?

As of Spring Data JPA 2.1 you can now configure a BootstrapMode (either via the @EnableJpaRepositories annotation or the XML namespace) that takes the following values: DEFAULT (default) — Repositories are instantiated eagerly unless explicitly annotated with @Lazy .

What is the use of LocalContainerEntityManagerFactoryBean?

The LocalContainerEntityManagerFactoryBean gives full control over EntityManagerFactory configuration and is appropriate for environments where fine-grained customization is required. The LocalContainerEntityManagerFactoryBean will create a PersistenceUnitInfo based on the persistence.

What is spring boot EntityScan?

@EntityScan annotation is used when entity classes are not placed in the main application package or its sub-packages. In this situation, we would declare the package or list of packages in the main configuration class within @EntityScan annotation.

How do I enable Jparepository in spring boot?

To activate the Spring JPA repository support, we can use the @EnableJpaRepositories annotation and specify the package that contains the DAO interfaces: @EnableJpaRepositories(basePackages = "com.


2 Answers

From the Spring Boot reference...

62.4 Separate @Entity definitions from Spring configuration

Spring Boot tries to guess the location of your @Entity definitions, based on the @EnableAutoConfiguration it finds. To get more control, you can use the @EntityScan annotation, e.g.

@Configuration @EnableAutoConfiguration @EntityScan(basePackageClasses=City.class) public class Application {    //... } 
like image 114
Emerson Farrugia Avatar answered Oct 15 '22 11:10

Emerson Farrugia


Using the following also helps:

@EntityScan("org.soluvas.buzz.core.jpa") 

It is an alias for 'basePackages' property that configures a list of packages to scan for annotated entities.

This annotation provides an alternative to manually setting LocalContainerEntityManagerFactoryBean.setPackagesToScan(String...) and is particularly useful if you want to configure entity scanning in a type-safe way, or if your LocalContainerEntityManagerFactoryBean is auto-configured.

like image 36
wdonet Avatar answered Oct 15 '22 09:10

wdonet