When writing our Spring application we will usually have entity classes – those annotated with @Entity annotation. We can consider two approaches to placing our entity classes:
- Under the application main package or its sub-packages
- Use a completely different root package
In the first scenario, we could use @EnableAutoConfiguration to enable Spring to auto-configure the application context.
In the second scenario, we would provide our application with the information where these packages could be found. For this purpose, we would use @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. This will tell Spring where to find entities used in our application:
@Configuration
@EntityScan("com.baeldung.demopackage")
public class EntityScanDemo {
// ...
}