MappingInstantiationException with a BigInteger field in Spring Data Couchbase

旧巷老猫 提交于 2019-12-12 03:47:36

问题


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

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

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: Failed to instantiate java.math.BigInteger using constructor NO_CONSTRUCTOR with arguments

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

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

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)


回答1:


You are right, there is no default converter for BigInteger in Spring Data Couchbase :(

We have custom converters around dates in DatesConverters but that's about it...

We could probably add a few converters in the same line as in the Spring Data Mongo module (like here). Opened the DATACOUCH-234 ticket to track that.

In the meantime, you can use the CustomConverters class instantiated in the AbstractCouchbaseConfiguration to provide your own converters. The simplest is to make a BigInteger-to-String converter for writes, and String-to-BigInteger to read it back.




回答2:


@Simon,

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.

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?

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.

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


来源:https://stackoverflow.com/questions/37671068/mappinginstantiationexception-with-a-biginteger-field-in-spring-data-couchbase

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!