Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB No converter found capable of converting from type org.bson.types.ObjectId to type java.lang.Long

Tags:

spring

mongodb

I have the following Model for a Customer that is used by Postgres and MongoDB. It works fine with Postgres but I get this error when I want to list all Customers from MongoDB:

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

Here is my model class:

package com.example.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;

import org.apache.commons.lang.builder.ToStringBuilder;

@Entity
@XmlRootElement(name = "customer")
@Table(name = "customer")
public class Customer implements java.io.Serializable {

    private static final long serialVersionUID = 1L;
    private Long id;
    private String firstName;
    private String lastName;

    public Customer() {
    }

    public Customer(String fn, String ln) {
        this.firstName = fn;
        this.lastName = ln;
    }

    public Customer(Long id, String firstName, String lastName) {
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
    }

    @XmlAttribute(name = "id", required = false)
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", unique = true, nullable = false)
    public Long getId() {
        return this.id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @XmlAttribute(name = "first-name", required = false)
    @Column(name = "first_name", nullable = false)
    public String getFirstName() {
        return this.firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }


    @XmlAttribute(name = "last-name", required = false)
    @Column(name = "last_name", nullable = false)
    public String getLastName() {
        return this.lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }
}

Here is my CustomerService to retrieve all Customers:

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import org.springframework.context.annotation.Import;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import com.example.model.Customer;
import com.example.service.CustomerService;

@Service
@Import({ MongoConfiguration.class })
public class MongoCustomerService implements CustomerService{   

    @Inject MongoTemplate mongoTemplate;

    Class<Customer> entityClass = Customer.class;

    @Override
    public Collection<Customer> getAllCustomers() {
        try {
             List<Customer> allCustomers = mongoTemplate.findAll(entityClass);
             return allCustomers;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

My pom.xml:

          </dependency>
            <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-mongodb</artifactId>
            <version>1.3.3.RELEASE</version>
        </dependency>
like image 503
Thys Andries Michels Avatar asked Jan 10 '14 04:01

Thys Andries Michels


2 Answers

I changed the id from Long to BigInteger and it worked as per the documentation.

like image 181
Thys Andries Michels Avatar answered Oct 11 '22 19:10

Thys Andries Michels


Either your id field needs to be called _id, or you need to annotate it with @Id. See the documentation.

like image 39
Trisha Avatar answered Oct 11 '22 19:10

Trisha