简体   繁体   中英

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:

@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.

UPDATE 2: Document

{ 
    "_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:

private List<Integer> fk_parts;

should be:

private List<ObjectId> fk_parts;

Also note that private int fk_properties; is mapped to nothing. If you want it to map to fk_product_property as I suspect, it should be:

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.

您的“外键”应使用@DbRef注释

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM