Skip to content
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

Multiple datasources with JPA repositories fails with generated AOT artifacts #29595

Closed
svenm1982 opened this issue Nov 28, 2022 · 4 comments
Closed
Labels
in: data Issues in data modules (jdbc, orm, oxm, tx) status: duplicate A duplicate of another issue theme: aot An issue related to Ahead-of-time processing

Comments

@svenm1982
Copy link

svenm1982 commented Nov 28, 2022

Overview

Spring Boot Version: >= 3.x.x

I have a project with multiple JPA DataSources over multiple configuration classes using @EnableJpaRepositories.

The project runs normally with Spring Boot 3.0.0 without native support, but when I generate the AOT classes and run with -Dspring.aot.enabled=true, the repository beans can not be resolved and the application does not start.

2022-11-28T09:48:17.201+01:00  INFO 17832 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2022-11-28T09:48:17.215+01:00  INFO 17832 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2022-11-28T09:48:17.216+01:00  INFO 17832 --- [           main] o.apache.catalina.core.StandardEngine    : Starting Servlet engine: [Apache Tomcat/10.1.1]
2022-11-28T09:48:17.314+01:00  INFO 17832 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2022-11-28T09:48:17.315+01:00  INFO 17832 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 669 ms
2022-11-28T09:48:17.457+01:00  INFO 17832 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [name: another]
2022-11-28T09:48:17.539+01:00  INFO 17832 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate ORM core version 6.1.5.Final
2022-11-28T09:48:17.746+01:00  WARN 17832 --- [           main] org.hibernate.orm.deprecation            : HHH90000021: Encountered deprecated setting [javax.persistence.sharedCache.mode], use [jakarta.persistence.sharedCache.mode] instead
2022-11-28T09:48:17.931+01:00  INFO 17832 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2022-11-28T09:48:18.112+01:00  INFO 17832 --- [           main] com.zaxxer.hikari.pool.HikariPool        : HikariPool-1 - Added connection conn0: url=jdbc:h2:mem:anotherdb user=SA
2022-11-28T09:48:18.114+01:00  INFO 17832 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2022-11-28T09:48:18.153+01:00  INFO 17832 --- [           main] SQL dialect                              : HHH000400: Using dialect: org.hibernate.dialect.H2Dialect
2022-11-28T09:48:18.922+01:00  INFO 17832 --- [           main] o.h.e.t.j.p.i.JtaPlatformInitiator       : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2022-11-28T09:48:18.931+01:00  INFO 17832 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'another'
2022-11-28T09:48:18.946+01:00  INFO 17832 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [name: maindatabase]
2022-11-28T09:48:18.951+01:00  WARN 17832 --- [           main] org.hibernate.orm.deprecation            : HHH90000021: Encountered deprecated setting [javax.persistence.sharedCache.mode], use [jakarta.persistence.sharedCache.mode] instead
2022-11-28T09:48:18.961+01:00  INFO 17832 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-2 - Starting...
2022-11-28T09:48:18.963+01:00  INFO 17832 --- [           main] com.zaxxer.hikari.pool.HikariPool        : HikariPool-2 - Added connection conn10: url=jdbc:h2:mem:maindb user=SA
2022-11-28T09:48:18.963+01:00  INFO 17832 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-2 - Start completed.
2022-11-28T09:48:18.964+01:00  INFO 17832 --- [           main] SQL dialect                              : HHH000400: Using dialect: org.hibernate.dialect.H2Dialect
2022-11-28T09:48:18.997+01:00  INFO 17832 --- [           main] o.h.e.t.j.p.i.JtaPlatformInitiator       : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2022-11-28T09:48:18.998+01:00  INFO 17832 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'maindatabase'
2022-11-28T09:48:19.002+01:00  WARN 17832 --- [           main] w.s.c.ServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'demoRestController': Unsatisfied dependency expressed through constructor parameter 0: No qualifying bean of type 'com.example.aopmultijpa.databases.maindatabase.repositories.MainTableRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
2022-11-28T09:48:19.002+01:00  INFO 17832 --- [           main] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'maindatabase'
2022-11-28T09:48:19.004+01:00  INFO 17832 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-2 - Shutdown initiated...
2022-11-28T09:48:19.007+01:00  INFO 17832 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-2 - Shutdown completed.
2022-11-28T09:48:19.007+01:00  INFO 17832 --- [           main] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'another'
2022-11-28T09:48:19.007+01:00  INFO 17832 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...
2022-11-28T09:48:19.008+01:00  INFO 17832 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown completed.
2022-11-28T09:48:19.011+01:00  INFO 17832 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2022-11-28T09:48:19.047+01:00 ERROR 17832 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of constructor in com.example.aopmultijpa.service.DemoRestController required a bean of type 'com.example.aopmultijpa.databases.maindatabase.repositories.MainTableRepository' that could not be found.


Action:

Consider defining a bean of type 'com.example.aopmultijpa.databases.maindatabase.repositories.MainTableRepository' in your configuration.

Steps to Reproduce

I created an demo/sample project (multijpa.zip) with two H2 databases and two configs with one repository and an entity each and a tiny RestController which will autowire the two repositories.

  1. extract the attached gradle project zip
  2. run graddle aotClasses generation
  3. Start MultijpaApplication with -Dspring.aot.enabled=true JVM Parameter
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Nov 28, 2022
@sbrannen sbrannen added the theme: aot An issue related to Ahead-of-time processing label Nov 28, 2022
@sbrannen sbrannen added this to the Triage Queue milestone Nov 28, 2022
@pnijem
Copy link

pnijem commented Dec 15, 2022

Hi

I am also having a similar issue, which I've been trying to solve for two days already. In my case it is not detecting the Entity. Thus, it is throwing UnsatisfiedDependencyException

The SomeEntity (annotated by @entity) is used in SomeRepository (annotated by @repository). Also in my case, the project is able to start in SB 3.0.0 without the native support.

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'rabbitMQConsumer': Unsatisfied dependency expressed through constructor parameter 0: Error creating bean with name 'someService': Unsatisfied depend
ency expressed through constructor parameter 0: Error creating bean with name 'someRepository': Not a managed type: class com.someCompany.model.SomeEntity
        at org.springframework.beans.factory.aot.BeanInstanceSupplier.resolveArgument(BeanInstanceSupplier.java:351)
        at org.springframework.beans.factory.aot.BeanInstanceSupplier.resolveArguments(BeanInstanceSupplier.java:271)
        at org.springframework.beans.factory.aot.BeanInstanceSupplier.get(BeanInstanceSupplier.java:206)
        at com.someCompany.SomeApplication.main(SomeApplication.java:26)
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'someService': Unsatisfied dependency expressed through constructor parameter 0: Error creating bean with name 'someRepository': Not 
a managed type: class com.someCompany.model.SomeEntity
        at org.springframework.beans.factory.aot.BeanInstanceSupplier.resolveArgument(BeanInstanceSupplier.java:351)
        at org.springframework.beans.factory.aot.BeanInstanceSupplier.resolveArguments(BeanInstanceSupplier.java:271)
        at org.springframework.beans.factory.aot.BeanInstanceSupplier.get(BeanInstanceSupplier.java:206)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'someRepository': Not a managed type: class com.someCompany.model.SomeEntity
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1751)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:599)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:521)
Caused by: java.lang.IllegalArgumentException: Not a managed type: class com.someCompany.model.SomeEntity
        at org.hibernate.metamodel.model.domain.internal.JpaMetamodelImpl.managedType(JpaMetamodelImpl.java:181)
        at org.hibernate.metamodel.model.domain.internal.MappingMetamodelImpl.managedType(MappingMetamodelImpl.java:496)
        at org.hibernate.metamodel.model.domain.internal.MappingMetamodelImpl.managedType(MappingMetamodelImpl.java:99)
        at org.springframework.data.jpa.repository.support.JpaMetamodelEntityInformation.<init>(JpaMetamodelEntityInformation.java:77)

@rstoyanchev rstoyanchev removed this from the Triage Queue milestone Jan 20, 2023
@sbrannen
Copy link
Member

sbrannen commented Jan 31, 2023

@pnijem, the behavior you have described sounds like it might be a separate issue.

Would you mind creating a new issue to discuss that particular failure (including a sample application which demonstrates the behavior)?

@sbrannen sbrannen added the in: data Issues in data modules (jdbc, orm, oxm, tx) label Jan 31, 2023
@sbrannen sbrannen changed the title Multi JPA with Repositories with generated AOT not starting Multiple datasources with JPA repositories fails with generated AOT artifacts Jan 31, 2023
@snicoll
Copy link
Member

snicoll commented May 15, 2023

Thanks for the report and the sample @svenm1982 and sorry it took so long. There's now a fix for this in 6.0.10-SNAPSHOT. Unfortunately, the fix didn't make it in time for Spring Boot 3.0.7 to be scheduled this week but it'll be available in the next one in June.

Duplicates #30476.

There might be some work needed in terms of supporting multiple datasources, see spring-projects/spring-data-jpa#2753 (comment).

@snicoll snicoll closed this as not planned Won't fix, can't repro, duplicate, stale May 15, 2023
@snicoll snicoll added status: duplicate A duplicate of another issue and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels May 15, 2023
@DigitalMediageek
Copy link

I don't know if it helps but I came across the "not a managed type" error when running a native spring-boot app with multiple databases, too. Turns out to be a configuration issue on my side. See issue 39266. Here is my sample repo of a working native application which demonstrates how to setup custom datasources and run multiple databases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: data Issues in data modules (jdbc, orm, oxm, tx) status: duplicate A duplicate of another issue theme: aot An issue related to Ahead-of-time processing
Projects
None yet
Development

No branches or pull requests

7 participants