[英]No converter found capable of converting from type org.bson.types.ObjectId to type int
I am using Spring data and mongodb to get all Products
using this function: 我正在使用Spring数据和mongodb使用此功能来获取所有
Products
:
@Repository
public class ProductDao {
@Autowired
private MongoOperations mongoOperations;
public List<Product> getAll() {
return mongoOperations.findAll(Product.class);
}
}
My 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;
}
Error: 错误:
org.springframework.core.convert.ConverterNotFoundException:
No converter found capable of converting from type org.bson.types.ObjectId to type int
How to fix it? 如何解决?
UPDATE: 更新:
I do have spring-core-4.1.0.RELEASE.jar
in lib folder which suppose to include required converter. 我确实在lib文件夹中有
spring-core-4.1.0.RELEASE.jar
该文件夹应该包含必需的转换器。
UPDATE 2: Document 更新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")
]
}
You are trying to implicitly convert an ObjectId to an Integer: 您尝试将ObjectId隐式转换为Integer:
private List<Integer> fk_parts;
should be: 应该:
private List<ObjectId> fk_parts;
Also note that private int fk_properties;
还要注意
private int fk_properties;
is mapped to nothing. 没有映射。 If you want it to map to
fk_product_property
as I suspect, it should be: 如果我想让它映射到
fk_product_property
,它应该是:
private ObjectId fk_product_property
or 要么
@Field("fk_product_property") private ObjectId fk_properties;
In any case that field also should be mapped to ObjectId
as well. 无论如何,该字段也应映射到
ObjectId
。
您的“外键”应使用@DbRef注释
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.