簡體   English   中英

找不到能夠將org.bson.types.ObjectId類型轉換為int類型的轉換器

[英]No converter found capable of converting from type org.bson.types.ObjectId to type int

我正在使用Spring數據和mongodb使用此功能來獲取所有Products

@Repository
public class ProductDao  {

    @Autowired
    private MongoOperations mongoOperations;
    public List<Product> getAll() {
            return mongoOperations.findAll(Product.class);
        }
}

我的產品類別:

@Document(collection = Product.COLLECTION_NAME)
public class Product implements Serializable {

    public Product() {
    }

    public static final String COLLECTION_NAME = "product";

    @Id
    private String _id;
    private String name;
    private DateTime date_time;
    private int fk_properties;
    private List<Integer> fk_parts;
}

錯誤:

    org.springframework.core.convert.ConverterNotFoundException: 
No converter found capable of converting from type org.bson.types.ObjectId to type int

如何解決?

更新:

我確實在lib文件夾中有spring-core-4.1.0.RELEASE.jar該文件夾應該包含必需的轉換器。

更新2:文檔

{ 
    "_id" : ObjectId("5449567cdf97f277c50d1ce2"), 
    "name" : "2014 ISF", 
    "auction_start" : ISODate("2014-12-08T12:00:00.000+0200"), 
    "auction_end" : ISODate("2014-12-08T14:00:00.000+0200"), 
    "listed" : "F", 
    "fk_product_property" : ObjectId("5229567cdf97f277c50d1ce2"), 
    "fk_parts" : [
        ObjectId("5339567cdf97f277c50d1ce2"), 
        ObjectId("5349567cdf97f277c50d1ce2")
    ]
}

您嘗試將ObjectId隱式轉換為Integer:

private List<Integer> fk_parts;

應該:

private List<ObjectId> fk_parts;

還要注意private int fk_properties; 沒有映射。 如果我想讓它映射到fk_product_property ,它應該是:

private ObjectId fk_product_property

要么

@Field("fk_product_property") private ObjectId fk_properties;

無論如何,該字段也應映射到ObjectId

您的“外鍵”應使用@DbRef注釋

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM