Open abcfy2 opened 6 years ago
Is the field orderId
defined as int
or Integer
in the domain class?
No, the params is a Map
which type is JsonbMapType
class Domain {
Map params
static mapping = {
params type: JsonbMapType
}
}
And seems that save()
works very well.
Domain domain = new Domain()
domain.params = [orderId: 809]
domain.save()
And in postgresql this column is :
{"orderId": 890}
But when I read it from domain ,the int value is float:
Domain doman = Domain.first()
domain.params.orderId // it's 890.0
New discovery, I find sometimes int value saved to jsonb will convert to float.
Here is my SQL:
> SELECT operation_params ->> 'orderId' FROM domain;
314
354
7.0
247.0
250
251
252.0
244
227.0
248.0
316
249.0
254
319
357
291.0
317.0
(361 rows)
But in fact all id should be int.
domain.operationParams = [orderId: id] // id is int
domain.save()
I second that. Very annoying, took me a while to track that down...
It looks like a problem with GSON. Please see https://github.com/google/gson/pull/1290 The class net.kaleidos.hibernate.usertype.JsonMapType needs to be changed on line 24 from: private final Gson gson = new GsonBuilder().serializeNulls().create() to Gson gson = new GsonBuilder().serializeNulls().setObjectToNumberStrategy(ToNumberPolicy.LONG_OR_DOUBLE).create();
I use
5.2.0
in grails3.3.3
.Here is my jsonb velue in database:
But when I get from domain, I find it has been converted to float, why ?