Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Data Mongodb Repositories don't implement inheritance correctly

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 had 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.

Has anyone encountered this bug and has any solution on how to fix it?

like image 497
Mateusz Rasiński Avatar asked Jan 19 '15 13:01

Mateusz Rasiński


1 Answers

I encounter the same issue.

Take a look at the source code and at my surprise it is kind of not implemented. It adds the Collection name and the entity class but not inserted in the final query the _class property. And after taking a look at it I realized that how Mongo would know that SubClass1 or Subclass2 derived from SuperClass. So I just override the SimpleMongoRepository Class and create my own Factory that put that class instead of the default SimpleMongoRepository

Here what i added:

public MySimpleMongoRepository(MongoEntityInformation<T, ID> metadata, MongoOperations mongoOperations) {

    Assert.notNull(mongoOperations);
    Assert.notNull(metadata);

    this.entityInformation = metadata;
    this.mongoOperations = mongoOperations;
    Reflections reflections = new Reflections("com.cre8techlabs.entity");
    Set<String> subTypes = reflections.getSubTypesOf(entityInformation.getJavaType()).stream().map(Class::getName).collect(Collectors.toSet());
    subTypes.add(entityInformation.getJavaType().getName());
    this.baseClassQuery = Criteria.where("_class").in(subTypes.toArray());
}

And here an example of implementation of a find

public T findOne(ID id) {
    Assert.notNull(id, "The given id must not be null!");
    Query q = getIdQuery(id).addCriteria(baseClassQuery);

    return mongoOperations.findOne(q, entityInformation.getJavaType(), entityInformation.getCollectionName());
}

It works for me, I am just afraid that it take a little longer

like image 75
Ly Kou NHIAYI Avatar answered Sep 25 '22 19:09

Ly Kou NHIAYI