我们正在使用Spring Data for Mongo DB 1.3.3,并且在检索其中一个嵌套对象是abstract类型的文档时会遇到异常。 如果我们将User类更改为具体,则不会抛出异常并且代码正常工作。
BSON结构:
{ "_id" : { "$oid" : "52d99dec88c6bd5da9c05a86"},
"_class": "com.xyz.Quote" ,
"name" : "sssds" ,
"quoteNumber" : 1006 ,
"account" : { "accountNumber" : "060926" ,
"phone" : [ ] , "fax" : [ ] ,
"leadDealerAccount" : "066418" ,
................................................
"quoteCreatedBy" : { "_class" : "com.xyz.DealerUser" ,
"lastName" : "TILLMAN" ,
"firstName" : "TIMOTHY" ,
"middleName" : "M"
}
}
POJO课程:
public abstract class User {
private String lastName;
private String firstName;
}
public class DealerUser extends User {
private Account primaryAccount;
}
public class AnotherUser extends User{
private List<Address> availableAddresses;
}
public class Quote {
private User quoteCreatedBy;
}
代码:
Query searchQuoteQuery = new Query(Criteria.where("account.leadDealerAccount"). is(066418));
List<Quote> mongoTemplate.find(searchQuoteQuery , Quote.class);
异常 最后一行抛出以下异常:
[2/7/14 12:54:14:019 CST] 00000032 SystemErr R org.springframework.data.mapping.model.MappingInst antiationException: Failed to instantiate com.deere.mp.common.domain.User using constructor public com.deere.mp.common.domain.User() with arguments
[2/7/14 12:54:14:020 CST] 00000032 SystemErr R at org.springframework.data.convert.ReflectionEntityI nstantiator.createInstance(ReflectionEntityInstantiator.java:78)
[2/7/14 12:54:14:020 CST] 00000032 SystemErr R at org.springframework.data.mongodb.core.convert.Mapp ingMongoConverter.read(MappingMongoConverter.java:243)
[2/7/14 12:54:14:020 CST] 00000032 SystemErr R at org.springframework.data.mongodb.core.convert.Mapp ingMongoConverter.read(MappingMongoConverter.java:223)
[2/7/14 12:54:14:020 CST] 00000032 SystemErr R at org.springframework.data.mongodb.core.convert.Mapp ingMongoConverter.readValue(MappingMongoConverter.java:1050)
[2/7/14 12:54:14:020 CST] 00000032 SystemErr R at org.springframework.data.mongodb.core.convert.Mapp ingMongoConverter.access$100(MappingMongoConverter.java:77)
[2/7/14 12:54:14:020 CST] 00000032 SystemErr R at org.springframework.data.mongodb.core.convert.Mapp ingMongoConverter$MongoDbPropertyValueProvider.get PropertyValue(MappingMongoConverter.java:999)
[2/7/14 12:54:14:020 CST] 00000032 SystemErr R at org.springframework.data.mongodb.core.convert.Mapp ingMongoConverter.getValueInternal(MappingMongoConverter.java:755)
[2/7/14 12:54:14:020 CST] 00000032 SystemErr R at org.springframework.data.mongodb.core.convert.Mapp ingMongoConverter$1.doWithPersistentProperty(MappingMongoConverter.java:256)
[2/7/14 12:54:14:020 CST] 00000032 SystemErr R at org.springframework.data.mongodb.core.convert.Mapp ingMongoConverter$1.doWithPersistentProperty(MappingMongoConverter.java:249)
[2/7/14 12:54:14:020 CST] 00000032 SystemErr R at org.springframework.data.mapping.model.BasicPersis tentEntity.doWithProperties(BasicPersistentEntity.java:257)
[2/7/14 12:54:14:020 CST] 00000032 SystemErr R at org.springframework.data.mongodb.core.convert.Mapp ingMongoConverter.read(MappingMongoConverter.java:249)
[2/7/14 12:54:14:021 CST] 00000032 SystemErr R at org.springframework.data.mongodb.core.convert.Mapp ingMongoConverter.read(MappingMongoConverter.java:223)
[2/7/14 12:54:14:021 CST] 00000032 SystemErr R at org.springframework.data.mongodb.core.convert.Mapp ingMongoConverter.read(MappingMongoConverter.java:187)
[2/7/14 12:54:14:021 CST] 00000032 SystemErr R at org.springframework.data.mongodb.core.convert.Mapp ingMongoConverter.read(MappingMongoConverter.java:183)
[2/7/14 12:54:14:021 CST] 00000032 SystemErr R at org.springframework.data.mongodb.core.convert.Mapp ingMongoConverter.read(MappingMongoConverter.java:77)
[2/7/14 12:54:14:021 CST] 00000032 SystemErr R at org.springframework.data.mongodb.core.MongoTemplat e$ReadDbObjectCallback.doWith(MongoTemplate.java:1975)
[2/7/14 12:54:14:021 CST] 00000032 SystemErr R at org.springframework.data.mongodb.core.MongoTemplat e.executeFindMultiInternal(MongoTemplate.java:1671)
[2/7/14 12:54:14:021 CST] 00000032 SystemErr R at org.springframework.data.mongodb.core.MongoTemplat e.doFind(MongoTemplate.java:1496)
[2/7/14 12:54:14:021 CST] 00000032 SystemErr R at org.springframework.data.mongodb.core.MongoTemplat e.doFind(MongoTemplate.java:1480)
[2/7/14 12:54:14:021 CST] 00000032 SystemErr R at org.springframework.data.mongodb.core.MongoTemplat e.find(MongoTemplate.java:527)
[2/7/14 12:54:14:021 CST] 00000032 SystemErr R at org.springframework.data.mongodb.core.MongoTemplat e.find(MongoTemplate.java:518)
[2/7/14 12:54:14:021 CST] 00000032 SystemErr R at com.deere.mp.quote.dao.impl.QuoteDAOImpl.findByUse rId(QuoteDAOImpl.java:137)
[2/7/14 12:54:14:021 CST] 00000032 SystemErr R at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[2/7/14 12:54:14:021 CST] 00000032 SystemErr R at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:60)
[2/7/14 12:54:14:021 CST] 00000032 SystemErr R at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)
答案 0 :(得分:0)
使用 @BsonDiscriminator 注释课程:
@org.bson.codecs.pojo.annotations.BsonDiscriminator()
public class DealerUser {}
此外,如果User
类是其他类的属性/字段,则必须使用 @BsonProperty(useDiscriminator = true)注释该字段:
public class Some class {
@org.bson.codecs.pojo.annotations.BsonProperty(useDiscriminator = true)
private User user;
}
答案 1 :(得分:-4)
在堆栈跟踪(以及Java基本逻辑)中,您无法在Java中实例化抽象类。那么你如何期望Spring Data API将JSON数据填充到无法实例化的类中? Spring将从mongodb获取数据并尝试将相同的数据推送到指定类的POJO(在您的情况下是抽象的并且无法实例化)。