Skip to content

Inconsistent type alias placement in documents with subclasses [DATAMONGO-1526] #2437

Open
@spring-projects-issues

Description

@spring-projects-issues

Tomasz Grabarczyk opened DATAMONGO-1526 and commented

The type alias _class is placed differently when saving a document and differently when creating a query, if the document contains a class that contains a subclass. This causes an incorrect query mismatch

Example: I have the following classes:

@Document
public class TestDocument {
	
	@Id private String id;
	private ClassA classA;

	public TestDocument(ClassA classA) {
		this.classA = classA;
	}
}
class ClassA {
	private ClassB classB;
	
	public ClassA(ClassB classB) {
		this.classB = classB;
	}
}
public abstract class ClassB { }
class ClassBImpl extends ClassB {
	
	private String field;

	public ClassBImpl(String field) {
		this.field = field;
	}
}

Now I run the following code:

ClassA classA = new ClassA(new ClassBImpl("value"));
mongoTemplate.insert(new TestDocument(classA));
Query query = Query.query(Criteria.where("classA").is(classA));
TestDocument result = mongoTemplate.findOne(query, TestDocument.class);
System.out.println(result);		// prints: null

I would expect that this query should return the document I inserted at the beginning, but it doesn't. The reason for that is that the ClassA is converted differently when saving document, and differently when creating the query:

Document saved to the database:

"_id" : ObjectId("5820521ff9854f15481ce52b"),
"_class" : "TestDocument",
"classA" : {
    "classB" : {
        "_class" : "ClassBImpl",
        "field" : "value"
    }
}

The query:

{ "classA" : { "classB" : { "field" : "value"}}}

(the query does not have the _class field)


Affects: 1.10 M1 (Ingalls), 1.9.4 (Hopper SR4)

Metadata

Metadata

Labels

in: mappingMapping and conversion infrastructuretype: bugA general bug

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions