In the following case, when I save CharFoo objects and then do a find , the generic field abstractFooField is converted back to a Character, but bar.field from the sub-document is converted to a String and not to a Character.
Am I doing something wrong? Or is this not supported by Spring Data MongoDB?
PS: I'm using spring-data-mongodb 1.6.0.RC1 and spring-data-commons 1.9.0.RC1
@Document(collection = "foo")
public abstract class AbstractFoo<T> {
@Id
private String id;
private T abstractFooField;
private AbstractBar<T> bar;
public AbstractFoo() {}
public AbstractFoo(T abstractFooField, AbstractBar<T> bar) {
this.abstractFooField = abstractFooField;
this.bar = bar;
}
}
public class CharFoo extends AbstractFoo<Character> {
public CharFoo() {}
public CharFoo(Character abstractFooField,
AbstractBar<Character> bar) {
super(abstractFooField, bar);
}
}
@Document
public class AbstractBar<T> {
public AbstractBar() {}
}
public class Bar<T> extends AbstractBar<T> {
private T field;
public Bar(T field) {
this.field = field;
}
}
Unit test:
@Test
public void givenGenericEntities_whenFindOne_thenReturnCorrectTypes() {
// GIVEN
Bar<Character> charBar = new Bar<>('A');
CharFoo charFoo = new CharFoo('B', charBar);
fooRepository.save(charFoo);
// WHEN
CharFoo dbCharFoo = charFooRepository.findOne(charFoo.getId());
// THEN
assertEquals("Field class should match", Character.class, dbCharFoo.getAbstractFooField().getClass());
assertEquals("Field class from sub-class should match", Character.class, ((Bar) dbCharFoo.getBar()).getField()
.getClass());
}
Result:
java.lang.AssertionError: Field class from sub-class should match
Expected :class java.lang.Character
Actual :class java.lang.String
MongoDB document:
{
"_id" : ObjectId("54db485a06e70e8444a15291"),
"_class" : "com.test.model.CharFoo",
"abstractFooField" : "B",
"bar" : {
"_class" : "com.test.model.Bar",
"field" : "A"
}
}
这是通过spring-data-mongodb 1.8.1中的DATAMONGO-1312解决的
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.