question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Spring Boot 2 MongoDB @TypeAlias does not work [DATAMONGO-2466]

See original GitHub issue

Markus 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:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:12 (2 by maintainers)

github_iconTop GitHub Comments

6reactions
mp911decommented, May 23, 2021

Please annotate your domain types with @Document. The type alias annotation isn’t sufficient to associate entity classes with the MongoMappingContext. The entity scan at application startup discovers and registers type aliases for proper instantiation.

0reactions
mp911decommented, Jun 7, 2021

Closing as entity scan of classes annotated with @Document works as designed.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found