使用 GORM 获取 Mongo 集合时,找不到能够从 java.lang.Double 类型转换为 java.lang.Long 类型的转换器

Posted

技术标签:

【中文标题】使用 GORM 获取 Mongo 集合时,找不到能够从 java.lang.Double 类型转换为 java.lang.Long 类型的转换器【英文标题】:No converter found capable of converting from type java.lang.Double to type java.lang.Long when fetching a Mongo collection using GORM 【发布时间】:2014-07-08 11:54:57 【问题描述】:

我的模型是:

package com.apposit.terra.connect.model

import java.util.Date;

import com.apposit.connect.security.User;
import com.apposit.terra.connect.FertilizerTypeEnum
import com.apposit.terra.connect.InputTypeEnum
import com.apposit.terra.connect.model.AbstractDomain

class UnionAllocation extends AbstractDomain 

    static mapWith = "mongo"

    Organization union
    InputTypeEnum inputType
    FertilizerTypeEnum fertilizerType
    CommodityType seedType
    InputCommodityType seedVariety
    Double requestedAmount = 0.0
    Double allocatedAmount = 0.0
    Double deliveredAmount = 0.0
    Double remaining  = 0.0
    Organization supplier
    Date expectedCompletionDate

    Date dateCreated
    Date lastUpdated
    User createdByUser
    User updatedByUser


    static constraints = 
        union(nullable: false )
        inputType(nullable: false)
        fertilizerType( nullable: true, unique: 'union')
        seedType( nullable: true)
        seedVariety( nullable: true, unique: 'union' )
        allocatedAmount( nullable: false)
        supplier( nullable: true)
        requestedAmount( nullable: true)
        createdByUser( nullable: true )
        updatedByUser( nullable: true )
    

    static mapping = 
        id generator: 'identity'
        sort id: "desc"
    

我的收藏看起来像:

/* 0 */

    "_id" : NumberLong(14),
    "allocatedAmount" : 450,
    "dateCreated" : ISODate("2014-03-07T05:22:16.832Z"),
    "deliveredAmount" : 0,
    "expectedCompletionDate" : ISODate("2014-03-17T21:00:00.000Z"),
    "inputType" : "SEED",
    "lastUpdated" : ISODate("2014-03-07T05:25:17.996Z"),
    "remaining" : 400,
    "requestedAmount" : 400,
    "seedType" : NumberLong(4),
    "seedVariety" : NumberLong(122),
    "supplier" : NumberLong(1406),
    "union" : NumberLong(1702),
    "version" : NumberLong(1)


/* 1 */

    "_id" : NumberLong(15),
    "allocatedAmount" : 800,
    "dateCreated" : ISODate("2014-03-07T14:17:02.006Z"),
    "deliveredAmount" : 0,
    "expectedCompletionDate" : ISODate("2014-03-19T21:00:00.000Z"),
    "inputType" : "SEED",
    "lastUpdated" : ISODate("2014-03-07T14:17:02.006Z"),
    "remaining" : 800,
    "requestedAmount" : 800,
    "seedType" : NumberLong(4),
    "seedVariety" : NumberLong(109),
    "supplier" : NumberLong(1406),
    "union" : NumberLong(1757),
    "version" : NumberLong(0)


/* 2 */

    "_id" : NumberLong(17),
    "allocatedAmount" : 12645.5,
    "dateCreated" : ISODate("2014-03-08T17:22:00.053Z"),
    "deliveredAmount" : 0,
    "expectedCompletionDate" : ISODate("2014-05-08T21:00:00.000Z"),
    "fertilizerType" : "DAP",
    "inputType" : "FERTILIZER",
    "lastUpdated" : ISODate("2014-03-08T17:24:08.372Z"),
    "remaining" : 126455,
    "requestedAmount" : 12434,
    "supplier" : NumberLong(1405),
    "union" : NumberLong(1702),
    "version" : NumberLong(1)


/* 3 */

    "_id" : NumberLong(19),
    "allocatedAmount" : 2210,
    "dateCreated" : ISODate("2014-03-08T17:27:21.759Z"),
    "deliveredAmount" : 0,
    "expectedCompletionDate" : ISODate("2014-05-08T21:00:00.000Z"),
    "fertilizerType" : "DAP",
    "inputType" : "FERTILIZER",
    "lastUpdated" : ISODate("2014-03-08T17:27:21.759Z"),
    "remaining" : 2210,
    "requestedAmount" : 0,
    "supplier" : NumberLong(1405),
    "union" : NumberLong(1617),
    "version" : NumberLong(0)


/* 4 */

    "_id" : NumberLong(20),
    "allocatedAmount" : 21068,
    "dateCreated" : ISODate("2014-03-08T17:29:25.274Z"),
    "deliveredAmount" : 0,
    "expectedCompletionDate" : ISODate("2014-05-08T21:00:00.000Z"),
    "fertilizerType" : "DAP",
    "inputType" : "FERTILIZER",
    "lastUpdated" : ISODate("2014-03-08T17:29:25.274Z"),
    "remaining" : 21068,
    "requestedAmount" : 16227,
    "supplier" : NumberLong(1405),
    "union" : NumberLong(1664),
    "version" : NumberLong(0)


/* 5 */

    "_id" : NumberLong(21),
    "allocatedAmount" : 3000,
    "dateCreated" : ISODate("2014-03-10T04:54:30.172Z"),
    "deliveredAmount" : 0,
    "expectedCompletionDate" : ISODate("2014-03-30T21:00:00.000Z"),
    "inputType" : "SEED",
    "lastUpdated" : ISODate("2014-03-10T04:54:30.172Z"),
    "remaining" : 3000,
    "requestedAmount" : 3000,
    "seedType" : NumberLong(6),
    "seedVariety" : NumberLong(93),
    "supplier" : NumberLong(1406),
    "union" : NumberLong(1430),
    "version" : NumberLong(0)


/* 6 */

    "_id" : NumberLong(22),
    "allocatedAmount" : 5000,
    "dateCreated" : ISODate("2014-03-11T13:25:09.123Z"),
    "deliveredAmount" : 0,
    "expectedCompletionDate" : ISODate("2014-03-21T21:00:00.000Z"),
    "inputType" : "SEED",
    "lastUpdated" : ISODate("2014-03-11T13:25:09.123Z"),
    "remaining" : 5000,
    "requestedAmount" : 5000,
    "seedType" : NumberLong(13),
    "seedVariety" : NumberLong(54),
    "supplier" : NumberLong(1406),
    "union" : NumberLong(2076),
    "version" : NumberLong(0)


/* 7 */

    "_id" : NumberLong(23),
    "allocatedAmount" : 5000,
    "createdByUser" : NumberLong(97),
    "dateCreated" : ISODate("2014-03-13T05:34:59.047Z"),
    "deliveredAmount" : 0,
    "expectedCompletionDate" : ISODate("2014-03-28T21:00:00.000Z"),
    "inputType" : "SEED",
    "lastUpdated" : ISODate("2014-03-13T05:34:59.047Z"),
    "remaining" : 5000,
    "requestedAmount" : 5000,
    "seedType" : NumberLong(4),
    "seedVariety" : NumberLong(110),
    "supplier" : NumberLong(1406),
    "union" : NumberLong(2079),
    "version" : NumberLong(0)


/* 8 */

    "_id" : NumberLong(24),
    "allocatedAmount" : 1000,
    "createdByUser" : NumberLong(97),
    "dateCreated" : ISODate("2014-03-13T06:10:40.031Z"),
    "deliveredAmount" : 300,
    "expectedCompletionDate" : ISODate("2014-05-31T21:00:00.000Z"),
    "inputType" : "SEED",
    "lastUpdated" : ISODate("2014-03-25T11:36:51.085Z"),
    "remaining" : 1000,
    "requestedAmount" : 1000,
    "seedType" : NumberLong(6),
    "seedVariety" : NumberLong(94),
    "supplier" : NumberLong(1407),
    "union" : NumberLong(1523),
    "version" : NumberLong(1)


/* 9 */

    "_id" : NumberLong(25),
    "allocatedAmount" : 6000,
    "createdByUser" : NumberLong(97),
    "dateCreated" : ISODate("2014-03-13T08:37:27.129Z"),
    "deliveredAmount" : 0,
    "expectedCompletionDate" : ISODate("2014-03-30T21:00:00.000Z"),
    "inputType" : "SEED",
    "lastUpdated" : ISODate("2014-03-13T08:37:27.129Z"),
    "remaining" : 6000,
    "requestedAmount" : 12000,
    "seedType" : NumberLong(6),
    "seedVariety" : NumberLong(94),
    "supplier" : NumberLong(1407),
    "union" : NumberLong(1496),
    "version" : NumberLong(0)


/* 10 */

    "_id" : NumberLong(26),
    "allocatedAmount" : 500,
    "createdByUser" : NumberLong(97),
    "dateCreated" : ISODate("2014-03-13T12:43:47.693Z"),
    "deliveredAmount" : 0,
    "expectedCompletionDate" : ISODate("2014-03-30T21:00:00.000Z"),
    "inputType" : "SEED",
    "lastUpdated" : ISODate("2014-03-13T12:43:47.693Z"),
    "remaining" : 500,
    "requestedAmount" : 500,
    "seedType" : NumberLong(6),
    "seedVariety" : NumberLong(92),
    "supplier" : NumberLong(1407),
    "union" : NumberLong(1496),
    "version" : NumberLong(0)


/* 11 */

    "_id" : NumberLong(27),
    "allocatedAmount" : 0,
    "createdByUser" : NumberLong(90),
    "dateCreated" : ISODate("2014-03-13T23:26:00.044Z"),
    "deliveredAmount" : 0,
    "expectedCompletionDate" : ISODate("2014-05-08T21:00:00.000Z"),
    "fertilizerType" : "DAP",
    "inputType" : "FERTILIZER",
    "lastUpdated" : ISODate("2014-03-13T23:26:00.044Z"),
    "remaining" : 0,
    "requestedAmount" : 34506,
    "supplier" : NumberLong(1405),
    "union" : NumberLong(2079),
    "version" : NumberLong(0)


/* 12 */

    "_id" : NumberLong(28),
    "allocatedAmount" : 0,
    "createdByUser" : NumberLong(90),
    "dateCreated" : ISODate("2014-03-13T23:31:04.347Z"),
    "deliveredAmount" : 0,
    "expectedCompletionDate" : ISODate("2014-05-08T21:00:00.000Z"),
    "fertilizerType" : "UREA",
    "inputType" : "FERTILIZER",
    "lastUpdated" : ISODate("2014-03-13T23:31:04.347Z"),
    "remaining" : 0,
    "requestedAmount" : 26952,
    "supplier" : NumberLong(1405),
    "union" : NumberLong(2079),
    "version" : NumberLong(0)


/* 13 */

    "_id" : NumberLong(29),
    "allocatedAmount" : 25000,
    "createdByUser" : 97,
    "dateCreated" : ISODate("2014-03-14T05:21:25.536Z"),
    "deliveredAmount" : 0,
    "expectedCompletionDate" : ISODate("2014-03-31T21:00:00.000Z"),
    "inputType" : "SEED",
    "lastUpdated" : ISODate("2014-03-14T05:21:25.536Z"),
    "remaining" : 25000,
    "requestedAmount" : 25000,
    "seedType" : NumberLong(13),
    "seedVariety" : NumberLong(83),
    "supplier" : NumberLong(2156),
    "union" : NumberLong(2079),
    "version" : NumberLong(0)


/* 14 */

    "_id" : NumberLong(30),
    "allocatedAmount" : 25000,
    "createdByUser" : 97,
    "dateCreated" : ISODate("2014-03-14T05:21:48.485Z"),
    "deliveredAmount" : 0,
    "expectedCompletionDate" : ISODate("2014-03-31T21:00:00.000Z"),
    "inputType" : "SEED",
    "lastUpdated" : ISODate("2014-03-14T05:21:48.485Z"),
    "remaining" : 25000,
    "requestedAmount" : 25000,
    "seedType" : NumberLong(13),
    "seedVariety" : NumberLong(84),
    "supplier" : NumberLong(2156),
    "union" : NumberLong(2079),
    "version" : NumberLong(0)


/* 15 */

    "_id" : NumberLong(31),
    "allocatedAmount" : 3000,
    "createdByUser" : 97,
    "dateCreated" : ISODate("2014-03-17T13:02:05.912Z"),
    "deliveredAmount" : 0,
    "expectedCompletionDate" : ISODate("2014-04-29T21:00:00.000Z"),
    "inputType" : "SEED",
    "lastUpdated" : ISODate("2014-03-17T13:02:05.912Z"),
    "remaining" : 3000,
    "requestedAmount" : 5000,
    "seedType" : NumberLong(4),
    "seedVariety" : NumberLong(126),
    "supplier" : NumberLong(1407),
    "union" : NumberLong(1686),
    "version" : NumberLong(0)


/* 16 */

    "_id" : NumberLong(32),
    "allocatedAmount" : 2000,
    "createdByUser" : 97,
    "dateCreated" : ISODate("2014-03-17T13:14:25.379Z"),
    "deliveredAmount" : 0,
    "expectedCompletionDate" : ISODate("2014-05-30T21:00:00.000Z"),
    "fertilizerType" : "DAP",
    "inputType" : "FERTILIZER",
    "lastUpdated" : ISODate("2014-03-17T13:14:25.379Z"),
    "remaining" : 2000,
    "requestedAmount" : 2000,
    "supplier" : NumberLong(1405),
    "union" : NumberLong(1686),
    "version" : NumberLong(0)


/* 17 */

    "_id" : NumberLong(33),
    "allocatedAmount" : 8000,
    "createdByUser" : NumberLong(97),
    "dateCreated" : ISODate("2014-03-19T10:28:02.631Z"),
    "deliveredAmount" : 0,
    "expectedCompletionDate" : ISODate("2014-06-29T21:00:00.000Z"),
    "fertilizerType" : "UREA",
    "inputType" : "FERTILIZER",
    "lastUpdated" : ISODate("2014-03-19T10:28:02.631Z"),
    "remaining" : 8000,
    "requestedAmount" : 10000,
    "supplier" : NumberLong(1405),
    "union" : NumberLong(1702),
    "version" : NumberLong(0)


/* 18 */

    "_id" : NumberLong(34),
    "allocatedAmount" : 5000,
    "createdByUser" : NumberLong(97),
    "dateCreated" : ISODate("2014-03-25T14:39:07.030Z"),
    "deliveredAmount" : 1000,
    "expectedCompletionDate" : ISODate("2014-06-29T21:00:00.000Z"),
    "fertilizerType" : "DAP",
    "inputType" : "FERTILIZER",
    "lastUpdated" : ISODate("2014-03-25T14:39:07.030Z"),
    "remaining" : 5000,
    "requestedAmount" : 10000,
    "supplier" : NumberLong(1405),
    "union" : NumberLong(1757),
    "version" : NumberLong(0)


/* 19 */

    "_id" : NumberLong(35),
    "allocatedAmount" : 6000,
    "createdByUser" : NumberLong(97),
    "dateCreated" : ISODate("2014-03-25T14:39:32.458Z"),
    "deliveredAmount" : 2000,
    "expectedCompletionDate" : ISODate("2014-06-29T21:00:00.000Z"),
    "fertilizerType" : "DAP",
    "inputType" : "FERTILIZER",
    "lastUpdated" : ISODate("2014-03-25T14:39:32.458Z"),
    "remaining" : 6000,
    "requestedAmount" : 10000,
    "supplier" : NumberLong(1405),
    "union" : NumberLong(1523),
    "version" : NumberLong(0)

当我UnionAllocation.list() 时,我得到:

org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type java.lang.Double to type java.lang.Long
    at org.grails.datastore.mapping.model.types.conversion.DefaultConversionService.convert(DefaultConversionService.java:21)
    at org.grails.datastore.mapping.engine.NativeEntryEntityPersister.refreshObjectStateFromNativeEntry(NativeEntryEntityPersister.java:478)
    at org.grails.datastore.mapping.mongo.engine.MongoEntityPersister.refreshObjectStateFromNativeEntry(MongoEntityPersister.java:110)
    at org.grails.datastore.mapping.mongo.engine.MongoEntityPersister.refreshObjectStateFromNativeEntry(MongoEntityPersister.java:78)
    at org.grails.datastore.mapping.engine.NativeEntryEntityPersister.createObjectFromNativeEntry(NativeEntryEntityPersister.java:379)
    at org.grails.datastore.mapping.mongo.query.MongoQuery$MongoResultList.convertDBObject(MongoQuery.java:993)
    at org.grails.datastore.mapping.mongo.query.MongoQuery$MongoResultList$1.next(MongoQuery.java:967)

【问题讨论】:

【参考方案1】:

我知道这是一个老问题,但它最终可以帮助某人。

无论如何,我遇到了类似的问题,我创建了自己的转换器来解决它。

public class LongToDateConverter implements Converter<Long, Date> 
    @Override
    public Date convert(Long lng) 
        if(lng!=null)
            return new Date(lng);
        else
            return null;
    

然后我注册为一个bean。

@Bean
@Override
public CustomConversions customConversions() throws Exception 
    List<Converter<?, ?>> converterList = new ArrayList<Converter<?, ?>>();
    converterList.add(new LongToDateConverter());

    return new CustomConversions(converterList);
  

更多详情,您可以在这里查看: https://docs.spring.io/spring-data/mongodb/docs/current/reference/html/#mapping-configuration

【讨论】:

【参考方案2】:

您是否手动更新了任何数据,即直接使用 mongodb shell? id 为 17 的数据行似乎已被污染。

【讨论】:

它和其他的有什么不同?

以上是关于使用 GORM 获取 Mongo 集合时,找不到能够从 java.lang.Double 类型转换为 java.lang.Long 类型的转换器的主要内容,如果未能解决你的问题,请参考以下文章

_id 找不到带有 Mongo 的 Sails.js

如何使用 GORM 为 Mongo 的低级 API 进行安全插入?

Grails Mongo GORM 插件 - 浮点和字节的映射是字符串而不是数字

使用 rmongodb 时,在 mongoDB 中看不到集合

Jest 找不到 MONGO_URI 的 .env.local

在 Express 中哪里可以找到 Mongo Collection?