Skip to content

AutoConfigurationPackage should register JPA entities #19024

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
chrylis opened this issue Nov 15, 2019 · 8 comments
Closed

AutoConfigurationPackage should register JPA entities #19024

chrylis opened this issue Nov 15, 2019 · 8 comments

Comments

@chrylis
Copy link
Contributor

chrylis commented Nov 15, 2019

With no explicit configuration at all, Boot will scan the package tree starting at the @EnableAutoConfiguration for JPA entities and register them automatically. @EntityScan can be used to provide Boot with a different root package to scan, but this disables the Boot auto-configuration scanning for entities.

When writing a starter that provides entities that should be included, there is currently no transparent way to register them with the persistence provider. Using @EntityScan on an auto-configuration class causes the default scan to back off, and @AutoConfigurationPackage does not register JPA entities (like it registers JPA and other Spring Data repositories).

Since JPA entities are part of the "Boot default auto-configure scan", using @AutoConfigurationPackage should cause the indicated package to be scanned for entities as well.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Nov 15, 2019
@wilkinsona wilkinsona changed the title AutoConfigurePackage should register JPA entities AutoConfigurationPackage should register JPA entities Nov 15, 2019
@wilkinsona
Copy link
Member

This should already happen. In the absence of any entity scan packages, JpaBaseConfiguration should scan every package registered with AutoConfigurationPackages:

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

It sounds like that's not working for you. If you'd like us to spend some more time investigating, please spend some time creating a minimal sample that demonstrates the behaviour that you have described and share it with us by zipping it up and attaching it to this issue.

@wilkinsona wilkinsona added the status: waiting-for-feedback We need additional information before we can continue label Nov 15, 2019
@chrylis
Copy link
Contributor Author

chrylis commented Nov 16, 2019

I'll give it a try. The project in question is a test harness anyway, so I'm optimistic.

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Nov 16, 2019
@wilkinsona wilkinsona added status: waiting-for-feedback We need additional information before we can continue and removed status: feedback-provided Feedback has been provided labels Nov 16, 2019
@spring-projects-issues
Copy link
Collaborator

If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.

@spring-projects-issues spring-projects-issues added the status: feedback-reminder We've sent a reminder that we need additional information before we can continue label Nov 23, 2019
@spring-projects-issues
Copy link
Collaborator

Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open the issue.

@spring-projects-issues spring-projects-issues removed status: waiting-for-feedback We need additional information before we can continue status: feedback-reminder We've sent a reminder that we need additional information before we can continue status: waiting-for-triage An issue we've not yet triaged labels Nov 30, 2019
@vlad-ti
Copy link

vlad-ti commented Mar 26, 2021

spring-entity-scan-issue.zip
I can confirm what @chrylis initially described. If you have a module "bar", which uses auto-configuration for detection of entities, and you have an application/module "foo" which depends on the module "bar", but has an @EntityScan annotation, then the auto configuration of the module "bar" won't discover its required entities. To resolve that, other both modules/applications have to have @EntityScan annotation, or both of them have to use auto-configuration without setting @EntityScan manually. This is quiet weird behavior, if you want to develop a library (e.g. module "bar") which intends to initialize its entities.

The linked zip above contains an example spring boot application which exemplifies the described problem. Steps to reproduce the issue:

  • Start the project via "mvn spring-boot:run" from your command line
  • Open URL "localhost:8080/foo" in your browser: you get "[]" as response, as the FooEntity was initialized
  • Open URL "localhost:8080/bar" in your browser: you get internal server error. On command line you will see QuerySyntaxException: BarEntity is not mapped
  • If you adapt the code, either by adding @EntityScan to BarConfiguration, or by removing @EntityScan from FooApplication, then the error gets resolved

@wilkinsona
Copy link
Member

In a nutshell, the problem described above was the following:

Since JPA entities are part of the "Boot default auto-configure scan", using @AutoConfigurationPackage should cause the indicated package to be scanned for entities as well.

Your sample demonstrates, when you remove @EntityScan from FooApplication, that this already works as described. Removing @EntityScan allows the @AutoConfigurationPackage that's present on BarConfiguration via its @EnableAutoConfiguration annotation to add to the packages that are scanned.

The intention is that you use @AutoConfigurationPackage when you want to make additive contributions to the packages that are scanned. This works well in a library module that wants to make additions to an application. Then, in the application itself, you can then allow any modules to contribute via their use of @AutoConfigurationPackage or you can use @EntityScan to take control over exactly which packages are scanned. If @EntityScan was additive, as I think you're suggesting, the ability to take control would be lost.

@vlad-ti
Copy link

vlad-ti commented Mar 26, 2021

I understand, that the ability of taking control for loading of all entities via @EntityScan might be sometimes demanded as well. However, then it gets tricky for those modules/applications which desire to rely on @AutoConfigurationPackage for entity scanning. If you have an application "foo" which relies on auto configuration, and some other modules, on which the "foo" depends, then if at least one of these modules (like a third-party library "bar") declares @EntityScan, then the whole application including needs to start using @EntityScan for its entities, as well as for all dependent modules which rely on auto-configuration.

@wilkinsona
Copy link
Member

wilkinsona commented Mar 26, 2021

A library that intends to contribute additional entities to an application shouldn't be using @EntityScan. If it does, then that's a bug in the library that should be fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants