【问题标题】:No converter found capable of converting from type org.bson.types.ObjectId to type int没有找到能够从 org.bson.types.ObjectId 类型转换为 int 类型的转换器
【发布时间】:2014-09-17 18:22:41
【问题描述】:

我正在使用 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")
    ]
}

【问题讨论】:

  • 产品集合中是否有id类型为整数的文档? db.product.find({"_id" : {$type : 16}})
  • 在命令行中执行db.product.find({"_id" : {$type : 16}})时什么都不返回...
  • 您能否发布您的示例文档,它将帮助我们确定数据类型不匹配的问题。

标签: java mongodb spring-data-mongodb


【解决方案1】:

您正在尝试将 ObjectId 隐式转换为 Integer:

private List&lt;Integer&gt; fk_parts;

应该是:

private List&lt;ObjectId&gt; fk_parts;

还要注意private int fk_properties; 被映射为空。如果您希望它映射到我怀疑的fk_product_property,它应该是:

private ObjectId fk_product_property

@Field("fk_product_property") private ObjectId fk_properties;

在任何情况下,该字段也应该映射到ObjectId

【讨论】:

    【解决方案2】:

    你的“外键”应该用@DbRef注解

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-28
      • 2018-02-15
      • 2020-11-19
      • 1970-01-01
      • 2019-10-20
      • 2019-02-10
      • 2021-09-22
      • 1970-01-01
      相关资源
      最近更新 更多