spring-projects / spring-data-jpa

Simplifies the development of creating a JPA-based data access layer.
https://spring.io/projects/spring-data-jpa/
Apache License 2.0
2.93k stars 1.39k forks source link

entity basePackages in third-party jars #3461

Closed en-o closed 1 month ago

en-o commented 1 month ago

There exists JPA operations in a custom third-party JAR. If @EntityScan is used, it fails to load entities from the third-party JAR, resulting in a "Not a managed type" error. However, if @EntityScan is not used, JPA scanning in the business project will be ineffective. Upon debugging, I found that this is overridden in org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration#getPackagesToScan(). Currently, I need to append instead of override the entity scanning. I'm unsure if this is a bug or a design principle. My current approach to handling this is as follows:

Override the original class override method

protected String[] getPackagesToScan() {
List<String> packages = EntityScanPackages.get(this.beanFactory).getPackageNames();
if (packages.isEmpty() && AutoConfigurationPackages.has(this.beanFactory)) {
    packages = AutoConfigurationPackages.get(this.beanFactory);
    packages.add("cn.jdevelops.config.standalone.model");
}
return StringUtils.toStringArray(packages);
}

image

odrotbohm commented 1 month ago

This is a known issue with Spring Boot. Prefer to use @AutoConfigurationPackage to add multiple packages to the default entity scanning.

en-o commented 1 month ago

This is a known issue with Spring Boot. Prefer to use @AutoConfigurationPackage to add multiple packages to the default entity scanning.

thanks