Spring Data Couchbase中具有BigInteger字段的MappingInstantiationException [英] MappingInstantiationException with a BigInteger field in Spring Data Couchbase

查看:171
本文介绍了Spring Data Couchbase中具有BigInteger字段的MappingInstantiationException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个实体,其字段类型为java.math.BigInteger

I have an entity with a field of type java.math.BigInteger

public class MyEntity {
  private String id;
  private BigInteger max;
}

使用Spring Data JPA将实体存储在DB中没有任何问题;但是当我检索实体时,出现以下异常

I don't have any problem in storing the entity in DB using Spring Data JPA; but when I retrieve the entity, I am getting the following exception

org.springframework.data.mapping.model.MappingInstantiationException:无法使用带有参数的构造函数NO_CONSTRUCTOR实例化java.math.BigInteger

org.springframework.data.mapping.model.MappingInstantiationException: Failed to instantiate java.math.BigInteger using constructor NO_CONSTRUCTOR with arguments

BigInteger没有无参数构造函数.那是问题吗?有什么办法解决这个问题?

BigInteger doesn't have a no-argument constructor. Is that a problem? Is there any way to solve this?

我正在使用spring数据bedbase,完整的堆栈跟踪如下所示

I am using spring data couchbase and the complete stack trace is as below

org.springframework.data.mapping.model.MappingInstantiationException: 无法使用构造函数实例化java.math.BigInteger 带参数的NO_CONSTRUCTOR 在org.springframework.data.convert.ReflectionEntityInstantiator.createInstance(ReflectionEntityInstantiator.java:64) 在org.springframework.data.convert.ClassGeneratingEntityInstantiator.createInstance(ClassGeneratingEntityInstantiator.java:83) 在org.springframework.data.couchbase.core.convert.MappingCouchbaseConverter.read(MappingCouchbaseConverter.java:203) 在org.springframework.data.couchbase.core.convert.MappingCouchbaseConverter.read(MappingCouchbaseConverter.java:185) 在org.springframework.data.couchbase.core.convert.MappingCouchbaseConverter.readValue(MappingCouchbaseConverter.java:725) 在org.springframework.data.couchbase.core.convert.MappingCouchbaseConverter.access $ 200(MappingCouchbaseConverter.java:65) 在org.springframework.data.couchbase.core.convert.MappingCouchbaseConverter $ CouchbasePropertyValueProvider.getPropertyValue(MappingCouchbaseConverter.java:78 在org.springframework.data.couchbase.core.convert.MappingCouchbaseConverter.getValueInternal(MappingCouchbaseConverter.java:243) 在org.springframework.data.couchbase.core.convert.MappingCouchbaseConverter $ 1.doWithPersistentProperty(MappingCouchbaseConverter.java:212) 在org.springframework.data.couchbase.core.convert.MappingCouchbaseConverter $ 1.doWithPersistentProperty(MappingCouchbaseConverter.java:206) 在org.springframework.data.mapping.model.BasicPersistentEntity.doWithProperties(BasicPersistentEntity.java:310) 在org.springframework.data.couchbase.core.convert.MappingCouchbaseConverter.read(MappingCouchbaseConverter.java:206) 在org.springframework.data.couchbase.core.convert.MappingCouchbaseConverter.read(MappingCouchbaseConverter.java:185) 在org.springframework.data.couchbase.core.convert.MappingCouchbaseConverter.read(MappingCouchbaseConverter.java:140) 在org.springframework.data.couchbase.core.convert.MappingCouchbaseConverter.read(MappingCouchbaseConverter.java:65) 在org.springframework.data.couchbase.core.CouchbaseTemplate.mapToEntity(CouchbaseTemplate.java:606) 在org.springframework.data.couchbase.core.CouchbaseTemplate.findById(CouchbaseTemplate.java:298) 在org.springframework.data.couchbase.repository.support.SimpleCouchbaseRepository.findOne(SimpleCouchbaseRepository.java:104)

org.springframework.data.mapping.model.MappingInstantiationException: Failed to instantiate java.math.BigInteger using constructor NO_CONSTRUCTOR with arguments at org.springframework.data.convert.ReflectionEntityInstantiator.createInstance(ReflectionEntityInstantiator.java:64) at org.springframework.data.convert.ClassGeneratingEntityInstantiator.createInstance(ClassGeneratingEntityInstantiator.java:83) at org.springframework.data.couchbase.core.convert.MappingCouchbaseConverter.read(MappingCouchbaseConverter.java:203) at org.springframework.data.couchbase.core.convert.MappingCouchbaseConverter.read(MappingCouchbaseConverter.java:185) at org.springframework.data.couchbase.core.convert.MappingCouchbaseConverter.readValue(MappingCouchbaseConverter.java:725) at org.springframework.data.couchbase.core.convert.MappingCouchbaseConverter.access$200(MappingCouchbaseConverter.java:65) at org.springframework.data.couchbase.core.convert.MappingCouchbaseConverter$CouchbasePropertyValueProvider.getPropertyValue(MappingCouchbaseConverter.java:78 at org.springframework.data.couchbase.core.convert.MappingCouchbaseConverter.getValueInternal(MappingCouchbaseConverter.java:243) at org.springframework.data.couchbase.core.convert.MappingCouchbaseConverter$1.doWithPersistentProperty(MappingCouchbaseConverter.java:212) at org.springframework.data.couchbase.core.convert.MappingCouchbaseConverter$1.doWithPersistentProperty(MappingCouchbaseConverter.java:206) at org.springframework.data.mapping.model.BasicPersistentEntity.doWithProperties(BasicPersistentEntity.java:310) at org.springframework.data.couchbase.core.convert.MappingCouchbaseConverter.read(MappingCouchbaseConverter.java:206) at org.springframework.data.couchbase.core.convert.MappingCouchbaseConverter.read(MappingCouchbaseConverter.java:185) at org.springframework.data.couchbase.core.convert.MappingCouchbaseConverter.read(MappingCouchbaseConverter.java:140) at org.springframework.data.couchbase.core.convert.MappingCouchbaseConverter.read(MappingCouchbaseConverter.java:65) at org.springframework.data.couchbase.core.CouchbaseTemplate.mapToEntity(CouchbaseTemplate.java:606) at org.springframework.data.couchbase.core.CouchbaseTemplate.findById(CouchbaseTemplate.java:298) at org.springframework.data.couchbase.repository.support.SimpleCouchbaseRepository.findOne(SimpleCouchbaseRepository.java:104)

推荐答案

@Simon,

BigInteger类型定义为要存储的实体的成员时,转换器解决方法将正常工作 (就像在原始问题中一样),但请考虑当实体包含Map<String, Object> 一个BigInteger.

The converter workaround will works just fine when the BigInteger type is defined as member of the entity you want to store (like in the original question), but consider the case when the entity contains a Map<String, Object> which holds a BigInteger.

写入转换器会将BigInteger存储为String,但是没有简单的方法来读取正确的 因为您再也没有可用的类型信息,所以请使用阅读转换器进行键入.

The writing converter will store the BigInteger as String, but there is no simple way of reading the correct type back with a reading converter since you don't have type information available anymore.

在这种情况下,最好的选择是遍历地图并尝试"以实例化数字.

The best bet in this case will be traverse the map and "try" to instantiate the number.

关于如何处理这种情况,您还有其他建议吗?

Do you have any other recommendation about how to handle this scenario?

我也不明白为什么不支持存储这些数据类型.据我了解,这不是Couchbase的问题 但有一个SDK/Spring数据限制.

Also I don't understand why storing these data types are not supported. Per my understanding is not a Couchbase problem but a SDK/Spring Data limitation.

将序列化为String(定义一个大数字")的JSON对象读入Map 支持Jackson,它可以将字段正确地映射到正确的类型.

Reading a JSON Object serialized as String which defines a "big number", into a Map with Jackson is supported and it correctly maps the field to the right type.

String json = "{\"bignumber\": 12345678901234567890}";
ObjectMapper om = new ObjectMapper();
Map<String, Object> map = om.readValue(json, Map.class);
System.out.println(map.get("bignumber") instanceof BigInteger); // True

这篇关于Spring Data Couchbase中具有BigInteger字段的MappingInstantiationException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆