Spring Boot 2 MongoDB @TypeAlias does not work [DATAMONGO-2466]
See original GitHub issueMarkus opened DATAMONGO-2466 and commented
In SpringBoot 1 TypeAlias(“name”) does work as expected but if I migrate to SpringBoot 2 it doesn’t instantiate the classes correctly.
@Document(collection = "test")
@TypeAlias("y")
public class TestY extends Test {
private String y;
public String getY() {
return y;
}
public void setY(String y) {
this.y = y;
}
}
@Document(collection = "test")
public class Test {
@Id
protected String id;
public Test() {
this.id = id;
}
}
@Document(collection = "test")
@TypeAlias("x")
public class TestX extends Test {
private String x;
public String getX() {
return x;
}
public void setX(String x) {
this.x = x;
}
}
Executions:
If I only load the instances -> Test instances should be TestX or TestY but they are all Test
List<Test> test = testRepository.findAll();
If I save before the entities -> Test instances after loading are instantiated right as TestX and TestY
TestY y = new TestY();
y.setY("y");
testRepository.save(y);
TestX x = new TestX();
x.setX("x");
testRepository.save(x);
List<Test> test = testRepository.findAll();
In Spring Boot 1 it works as expected.
Dependencies
before: spring-boot-starter-data-mongodb-1.5.21.RELEASE
after: spring-boot-starter-data-mongodb-2.1.11.RELEASE
Repositories
@Repository
public interface TestRepository extends MongoRepository<Test, String> {
}
Affects: 2.1.16 (Lovelace SR16)
1 votes, 2 watchers
Issue Analytics
- State:
- Created 4 years ago
- Reactions:2
- Comments:12 (2 by maintainers)
Top Results From Across the Web
Spring Boot 2 MongoDB @TypeAlias does not work
In SpringBoot 1 TypeAlias("name") does work as expected but if I migrate to SpringBoot 2 it doesn't instantiate the classes correctly.
Read more >Spring Data MongoDB - Reference Documentation
In this section, we try to provide what we think is an easy-to-follow guide for starting with the Spring Data MongoDB module. However,...
Read more >Springboot latest version unable to connect to the serverless ...
My spring-boot service, running behind the AWS load balancer, needs to connect to the Serverless MongoDB instance.
Read more >Polymorphic fields with MongoDB and Spring Data
So the 2 implementations are different and one cannot be deserialized from the other. Both implementations are also annotated with @TypeAlias .
Read more >Spring Data MongoDB — My take on inheritance support
The problem. For example, imagine a simple hierarchy of classes (get/set methods omitted): @Document(collection = "things")
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Please annotate your domain types with
@Document
. The type alias annotation isn’t sufficient to associate entity classes with theMongoMappingContext
. The entity scan at application startup discovers and registers type aliases for proper instantiation.Closing as entity scan of classes annotated with
@Document
works as designed.