Entity in Spring Data has Atomicinteger property causing issues

361 views
Skip to first unread message

Bilal Clarance

unread,
Apr 18, 2012, 6:33:59 PM4/18/12
to mongod...@googlegroups.com
Hi,

I am working on a project in which I am persisting legacy documents using MongoDB. My problem is that the entity that I am stored has a property of type AtomicInteger. When retrieving an entity from mongo I get this exception:

Unable to convert value "0" from type 'java.lang.Integer' to type 'java.util.concurrent.atomic.AtomicInteger'; nested exception is java.lang.IllegalArgumentException: Could not convert number [0] of type [java.lang.Integer] to unknown target class [java.util.concurrent.atomic.AtomicInteger]

Of course you cannot cast AtomicInteger to Integer, so I added a setter to the entity as follows:

    public void setErrorCount(Integer errorCount) {
        this.errorCount.set(errorCount);
    }

This did not solve the issue. I have spent quite a bit of time on google without luck. If anyone has some ideas I would be grateful.

Thanks
Bilal

Eliot Horowitz

unread,
Apr 19, 2012, 1:04:44 AM4/19/12
to mongod...@googlegroups.com
You can add a hook to convert AtomicInteger to Integer
See: http://api.mongodb.org/java/current/org/bson/BSON.html#addEncodingHook(java.lang.Class,
org.bson.Transformer)

> --
> You received this message because you are subscribed to the Google Groups
> "mongodb-user" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/mongodb-user/-/uTCe-7ZXsSMJ.
> To post to this group, send email to mongod...@googlegroups.com.
> To unsubscribe from this group, send email to
> mongodb-user...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/mongodb-user?hl=en.

Bilal Clarance

unread,
Apr 19, 2012, 5:26:57 PM4/19/12
to mongodb-user
Thank you for your answer and I led me to get a some code that would
do the job.
BSON.addDecodingHook(Integer.class, new Transformer() {
@Override
public Object transform(Object object) {
if(object instanceof Integer) {
Integer integer = (Integer) object;
return new AtomicInteger(integer);
}
return object;
}
});

I do have a problem with this though. My entity that I am getting has
other properties that are in fact Integer types so when it pulls those
values out it convert those as well. Could you guide me to somewhere
where I could figure out how to convert based on a specific property
name or based on what type the property of the Entity/Object that I
want to deserialize to from the document;

On Apr 18, 10:04 pm, Eliot Horowitz <el...@10gen.com> wrote:
> You can add a hook to convert AtomicInteger to Integer
> See:http://api.mongodb.org/java/current/org/bson/BSON.html#addEncodingHoo...,

schaffer

unread,
May 17, 2012, 3:38:30 PM5/17/12
to mongod...@googlegroups.com
I have a similar issue except I'm using arrays of Atomic Integers.
They are fine writing to the DB and they save as Int32.
However, when I try the solution you suggest I get the following error:

com.mongodb.BasicDBList cannot be cast to [[Ljava.util.concurrent.atomic.AtomicInteger;
java.lang.ClassCastException: com.mongodb.BasicDBList cannot be cast to [[Ljava.util.concurrent.atomic.AtomicInteger;


Any ideas on how to overcome this?

Thanks


Sample code:

BSON.addDecodingHook(Integer.class, new Transformer());

AtomicInteger[][] array = (AtomicInteger[][]) dbReturn.get(arrayName);



class Transformer implements org.bson.Transformer {

public Object transform(Object object) {

if (object instanceof Integer) {

Bryan

unread,
May 18, 2012, 3:13:08 AM5/18/12
to mongodb-user
The driver deserializes BSON arrays as Lists which can't be cast as a
multi-dimensional array. You'll need to iterate over the List to
populate the array.

schaffer

unread,
May 18, 2012, 4:20:16 AM5/18/12
to mongod...@googlegroups.com
Thanks. That's good to know.

Bryan

unread,
May 18, 2012, 2:14:19 PM5/18/12
to mongodb-user
The drivers try to map BSON types to driver specific native types,
while still providing flexibility to the developer. The encode/decode
hooks work at a low level within the driver, at the wire protocol
level. This gives you some latitude, but the transformation will be
applied across all objects targeted by the hook. Identifying and
modifying more sophisticated objects and structures by name will be
easier to do in your application once the DBObject has been created.
Reply all
Reply to author
Forward
0 new messages