【问题标题】:MongoDB No converter found capable of converting from type org.bson.types.ObjectId to type java.lang.LongMongoDB 没有找到能够从 org.bson.types.ObjectId 类型转换为 java.lang.Long 类型的转换器
【发布时间】:2014-01-28 22:55:16
【问题描述】:

我为 Postgres 和 MongoDB 使用的客户提供以下模型。它适用于 Postgres,但是当我想列出来自 MongoDB 的所有客户时出现此错误:

org.springframework.core.convert.ConverterNotFoundException: 否 发现能够从类型转换的转换器 org.bson.types.ObjectId 输入 java.lang.Long

这是我的模型类:

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);
    }
}

这是我的 CustomerService 来检索所有客户:

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;
        }
    }

我的 pom.xml:

          </dependency>
            <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-mongodb</artifactId>
            <version>1.3.3.RELEASE</version>
        </dependency>

【问题讨论】:

    标签: spring mongodb


    【解决方案1】:

    我将 id 从 Long 更改为 BigInteger,它按照文档工作。

    【讨论】:

    • @Trisha 的回答对我来说还不够。我还必须应用它才能使其工作。
    【解决方案2】:

    您的id 字段需要称为_id,或者您需要使用@Id 对其进行注释。见the documentation

    【讨论】:

    • 在我的情况下,我有一个自动生成的 _id 和一个我创建的 id,我得到了同样的错误。你有什么建议吗?
    【解决方案3】:

    以下是我为让 mongo 在 Grails(2.3.4) 上运行所做的三个步骤:

    我有带有 people 集合的 mongo db 实例。

    1。数据源.groovy

    ​​>
    grails {
        mongo {
            host = "localhost"
            port = 27017
            databaseName = "<db Name>"
        }
    }
    

    2。 BuildConfig.groovy

    ​​>
    plugins {
    
            // plugins for the build system only
            build ":tomcat:7.0.47"
    
            // plugins for the compile step
            compile ":scaffolding:2.0.1"
            compile ':cache:1.1.1'
            **compile ":mongodb:1.3.1"**
    
            // plugins needed at runtime but not for compilation
            runtime ":hibernate:3.6.10.6" // or ":hibernate4:4.1.11.6"
            runtime ":database-migration:1.3.8"
            runtime ":jquery:1.10.2.2"
            runtime ":resources:1.2.1"
         }
    

    3。域定义:

    package mongo
    class Person{
    String id
    Sting name
    
    static mapWith="mongo"
    
    static mapping={
     collection "persons"
     database "<dbName>"
    
    }
    }
    

    【讨论】:

      【解决方案4】:

      创建字段 _id 并使用 @Id 进行注释。将类型设为大整数

      【讨论】:

      • 请更新以包含一个简短的解释,说明为什么会这样。
      【解决方案5】:

      在我的表格中显示不同类型的 ID 时出现错误。删除所有错误的 ID 后,我的应用运行良好。

      【讨论】:

        【解决方案6】:
        import org.bson.types.ObjectId;
        

        那么 使用 ObjectId _id 而不是 Long id;

        @Id
        private ObjectId _id;
        

        【讨论】:

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