Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring MongoDB未找到能够从org.bson.types.ObjectId类型转换为java.lang.Long类型的转换器_Spring_Mongodb - Fatal编程技术网

Spring MongoDB未找到能够从org.bson.types.ObjectId类型转换为java.lang.Long类型的转换器

Spring MongoDB未找到能够从org.bson.types.ObjectId类型转换为java.lang.Long类型的转换器,spring,mongodb,Spring,Mongodb,我为Postgres和MongoDB使用的客户提供了以下模型。它在Postgres上运行良好,但当我想列出MongoDB的所有客户时,我会出现以下错误: org.springframework.core.convert.ConverterNotFoundException:否 发现转换器能够从类型转换 org.bson.types.ObjectId以键入java.lang.Long 这是我的模型课: package com.example.model; import javax.persist

我为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;
        }
    }
import java.util.ArrayList;
导入java.util.Collection;
导入java.util.List;
导入java.util.Map;
导入javax.inject.inject;
导入org.springframework.context.annotation.import;
导入org.springframework.data.domain.Sort;
导入org.springframework.data.domain.Sort.Direction;
导入org.springframework.data.mongodb.core.MongoTemplate;
导入org.springframework.data.mongodb.core.query.query;
导入org.springframework.stereotype.Service;
导入com.example.model.Customer;
导入com.example.service.CustomerService;
@服务
@导入({MongoConfiguration.class})
公共类MongoCustomerService实现CustomerService{
@注射MongoTemplate MongoTemplate;
Class entityClass=Customer.Class;
@凌驾
公共集合getAllCustomers(){
试一试{
列出所有客户=mongoTemplate.findAll(entityClass);
退回所有客户;
}捕获(例外e){
e、 printStackTrace();
返回null;
}
}
My pom.xml:

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

org.springframework.data
spring数据mongodb
1.3.3.1发布

您的
id
字段需要调用
\u id
,或者您需要用@id对其进行注释。请参见。

我将id从Long更改为BigInteger,并按照文档进行操作。

以下是我在Grails(2.3.4)上运行mongo所做的三个步骤: 我有mongo db实例和persons集合

1.DataSource.groovy 3.域名定义:
包mongo
班主任{
字符串id
刺名
静态mapWith=“mongo”
静态映射={
收集“人”
数据库“”
}
}

生成字段\u id并用@id注释。将类型设置为大整数

当我的表中出现不同类型的id时,我遇到了一个错误。删除所有错误id后,我的应用程序工作正常

import org.bson.types.ObjectId;
然后 使用ObjectId_id代替Long id

@Id
private ObjectId _id;

@Trisha的回答对我来说是不够的。我也必须应用这个来让它工作。请更新,包括一个小的解释为什么这工作。
package mongo
class Person{
String id
Sting name

static mapWith="mongo"

static mapping={
 collection "persons"
 database "<dbName>"

}
}
import org.bson.types.ObjectId;
@Id
private ObjectId _id;