spring-projects / spring-projects/spring-data-mongodb

Subclass Repository returns also Superclass objects [DATAMONGO-1142]

Open
#2,060 10 comments 0 reactions 1 assignee View on GitHub

@christophstrobl is already working on this.

Since Dec 30, 2020.

in: core in: repository type: bug
Dominant language
Java
Stars
1.7k
Forks
1.1k
PR merge metrics
No merged PRs in 30d

Description

Mateusz Rasiński opened DATAMONGO-1142 and commented

Having two types of entities, that are mapped to two Java classes in the single MongoDB collection:

@Document
public class Superclass { ... }

@Document(collection = "superclass")
public class Subclass extends Superclass { ... }

and two repositories for those entities:

public interface SuperclassRepository extends MongoRepository<Superclass, String> {}
public interface SubclassRepository extends MongoRepository<Subclass, String> {}

MongoRepositories don't handle the inheritance of the entities correctly. While querying for all Subclass objects (e.g. SubclassRepository.findAll()) the result set contains Superclass objects, that are instantiated (or at least have been tried to be instantiated) with null values for fields that are part of the Subclass, but are not part of the Superclass.

The expected result would be that SubclassRepository should return only Subclass objects, while SuperclassRepository should return Superclass and Subclass objects. It works this way in Spring Data JPA


Affects: 1.6.1 (Evans SR1)

Reference URL: http://stackoverflow.com/questions/28025710/spring-data-mongodb-repositories-dont-implement-inheritance-correctly

Referenced from: pull request https://github.com/spring-projects/spring-data-mongodb/pull/266

4 votes, 7 watchers

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.