A Spring Boot application declares `spring-boot-starter-data-jpa` and a single embedded H2 datasource, and defines JPA entity classes annotated with `@Entity`. The classes live in sub-packages beneath the package that holds the `@SpringBootApplication` class, and no `@EntityScan` annotation is present anywhere in the application. According to the Spring Boot reference documentation, how are those entity classes discovered?
A. They are not discovered; without an explicit `@EntityScan` or a `persistence.xml` listing each class, Spring Boot cannot build a JPA persistence unit.
Reflects the misconception that JPA in Spring Boot still requires a hand-maintained `persistence.xml` (or an explicit scan annotation). Spring Boot's auto-configuration deliberately removes that requirement — a `persistence.xml` is not needed and entity discovery happens automatically.
B. Spring Boot scans automatically, starting from the package that contains the `@SpringBootApplication` (or `@EnableAutoConfiguration`) class and including its sub-packages.Correct answer
Correct. The reference documentation states that classes annotated with `@Entity`, `@Embeddable`, or `@MappedSuperclass` are searched automatically, and by default all packages below the auto-configuration (main application) class are scanned — `@EntityScan` is only needed when entities live outside that root package.
C. They are discovered only if each entity class is also registered as a Spring bean via `@Component` or an equivalent stereotype, since entity scanning reuses the component-scanning bean registry.
Confuses entity scanning with component scanning. They are distinct mechanisms that happen to share a default root package: entities are located and registered with the `EntityManagerFactory` as managed persistent types, not instantiated as Spring beans, so no stereotype annotation is involved.
D. They are discovered only if they sit in the same package as the `@Repository` interfaces that use them, because entity scanning is driven from each repository's declared domain type.
Confuses repository scanning with entity scanning and invents a co-location rule. Entity discovery is package-scan based from the auto-configuration class and is entirely independent of where Spring Data repository interfaces are declared or what domain types they are parameterised with.
Explanation
Spring Boot's JPA auto-configuration removes the need to maintain a `persistence.xml`: it automatically searches for classes annotated with `@Entity`, `@Embeddable`, or `@MappedSuperclass`, and by default that search covers the package containing the auto-configuration (`@SpringBootApplication`/`@EnableAutoConfiguration`) class and everything beneath it, so entities in sub-packages are picked up with no extra annotation. `@EntityScan` exists only to redirect or widen that search when entities live outside the root package. Requiring a `persistence.xml` describes plain Java EE style bootstrapping rather than Boot's auto-configuration; requiring a stereotype annotation confuses entity scanning with component scanning, since entities are registered as persistent types with the `EntityManagerFactory` rather than as Spring beans; and tying discovery to repository packages confuses repository scanning with entity scanning, which is package-based and independent of any repository declaration.