2014-02-07 68 views
1

我們正在使用Spring Data for MongoDB 1.3.3,並且在檢索其中一個嵌套對象是abstract類型的文檔時發生異常。 如果我們將User類更改爲具體,那麼不會引發異常,並且代碼可以正常工作。嘗試實例化抽象類時MongoDB中的異常

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) 

回答

-2

它是那麼從堆棧跟蹤(以及Java基本邏輯),很明顯,你不能在Java中實例化一個抽象類。那麼你如何期待Spring Data API將JSON數據填充到無法實例化的類中? Spring會從mongodb中獲取數據,並嘗試將相同的數據轉換爲指定類的POJO(在您的情況下,這是抽象的,無法實例化)。

+0

有什麼辦法可以達到預期的行爲嗎? – Nayan