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

java.lang.InstantiationError after upgrading to Spring Boot 2.4.4 (spring data commons 2.4.6) #3608

Closed
svpr3m0 opened this issue Mar 23, 2021 · 8 comments
Labels
for: stackoverflow A question that's better suited to stackoverflow.com

Comments

@svpr3m0
Copy link

svpr3m0 commented Mar 23, 2021

In a Spring Boot application we use Spring Data Mongo to map some documents. Some of them have embedded documents with a hierarchy, e.g.:

@Document("projects")
@TypeAlias("project")
data class Project(
       ...
       val budget: ProjectBudget)
abstract class ProjectBudget {

    abstract fun getCurrentBudget(): MonetaryAmount?

}
@TypeAlias("projectProjectBudget")
class ClosedProjectBudget(... ) : ProjectBudget() {}

Up to Spring Boot 2.4.3 when retrieving a Project instance the corresponding ProjectBudget subclass what built correctly. However after the upgrade to Boot 2.4.4 we get this exception:

Caused by: java.lang.InstantiationError: xxx.ProjectBudget
	at xxx.ProjectBudget_Instantiator_srp3na.newInstance(Unknown Source)
	at org.springframework.data.mapping.model.ClassGeneratingEntityInstantiator$EntityInstantiatorAdapter.createInstance(ClassGeneratingEntityInstantiator.java:238)
	at org.springframework.data.mapping.model.ClassGeneratingEntityInstantiator.createInstance(ClassGeneratingEntityInstantiator.java:87)
	at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:344)

There seems to be a regression when instantiating subclasses.

@mp911de
Copy link
Member

mp911de commented Mar 23, 2021

This looks actually as if the subclass isn't registered with MappingContext. To properly resolve the target class using TypeAlias, it must be already present in the context.

Can you isolate the issue and provide a minimal reproducer?

@codependent
Copy link

Hi @mp911de, I've uploaded a sample project where you can reproduce the issue: https://github.com/codependent/mongo-sample2

The instructions are in the readme file.

@mp911de
Copy link
Member

mp911de commented Mar 25, 2021

ServiceProjectBudget and ProjectProjectBudget are not annotated with @Document hence these types aren't discovered during the application startup.

@mp911de mp911de closed this as completed Mar 25, 2021
@codependent
Copy link

@mp911de thanks for the suggestion, it solved the problem. What's funny is that with Spring Boot <=2.4.3 it was working without the annotations. Maybe it would be worth mentioning it in the docs.

@mp911de
Copy link
Member

mp911de commented Mar 25, 2021

That's interesting. I'm not sure why this is. Since Spring Boot does the entity scan, it's worth raising an issue there.

@wilkinsona
Copy link
Member

The Spring Boot 2.4.4-based sample works fine if I downgrade Spring Data to 2020.0.5 (the version used in Spring Boot 2.4.3). If I change the sample to be Spring Boot 2.4.3-based but with Spring Data upgraded to 2020.0.6 then the sample no longer works. As far as I can tell, it's something in Spring Data 2020.0.6 that's causing the problem.

@mp911de mp911de reopened this Mar 25, 2021
@mp911de
Copy link
Member

mp911de commented Mar 25, 2021

Let me have another look why this is.

@mp911de mp911de transferred this issue from spring-projects/spring-data-commons Mar 25, 2021
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Mar 25, 2021
@mp911de
Copy link
Member

mp911de commented Mar 25, 2021

I entirely missed the fact of the custom Mongo configuration MongoDbClientConfiguration hence I relied on Spring Boot to perform the entity scan.

I moved the ticket into Spring Data MongoDB. The difference comes from #3592 where we removed @Persistent as marker annotation from the entity scan in Spring Data MongoDB. Previously, this arrangement worked because @TypeAlias is meta-annotated with @Persistent and so entities annotated with @TypeAlias are picked up as entities during the scan.

ServiceProjectBudget and ProjectProjectBudget aren't subtypes of Project hence the @Document annotation doesn't apply these types. So annotating both types with @Document is the way to go and it worked previously rather by accident and not by intent.

@mp911de mp911de closed this as completed Mar 25, 2021
@mp911de mp911de added for: stackoverflow A question that's better suited to stackoverflow.com and removed status: waiting-for-triage An issue we've not yet triaged labels Mar 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
for: stackoverflow A question that's better suited to stackoverflow.com
Projects
None yet
Development

No branches or pull requests

5 participants