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

 伪祢添睬_362 发布于 2023-02-04 17:50

我为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 entityClass = Customer.class;

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

我的pom.xml:

          
            
            org.springframework.data
            spring-data-mongodb
            1.3.3.RELEASE
        

Thys Andries.. 7

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

1 个回答
  • 我将id从Long更改为BigInteger,并按照文档进行操作.

    2023-02-04 17:52 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有