ClassCastException on result.getSavedId()

244 views
Skip to first unread message

Andreas Lundblad

unread,
Apr 26, 2014, 9:42:23 AM4/26/14
to mongo-jack...@googlegroups.com
Hi everybody,

I'm trying to follow the MongoJack tutorial but I'm failing the first task: Inserting an object into the database.

This is what I have:


DB db
= new MongoClient().getDB("mydb");

JacksonDBCollection<MyDomainObject, String> coll =
   
JacksonDBCollection.wrap(db.getCollection("coll"),
                             
MyDomainObject.class,
                             
String.class);

MyDomainObject obj = new MyDomainObject(ObjectId.get().toString(), 123456789L);

WriteResult<MyDomainObject, String> result = coll.insert(obj);

System.out.println(result.getSavedId()); // Throws java.lang.ClassCastException: org.bson.types.ObjectId cannot be cast to java.lang.String




Where MyDomainObject class looks as follows:


class MyDomainObject {

   
// @org.mongojack.ObjectId  doesn't work
   
public String id;
   
public long someValue;

   
public MyDomainObject(String id, long someValue) {
       
this.id = id;
       
this.someValue = someValue;
   
}
}




I can't for my life figure out why. Any help appreciated.

best regards,
Andreas

Luke Palmer

unread,
Apr 27, 2014, 4:50:56 PM4/27/14
to mongo-jack...@googlegroups.com
Andreas,

In your example, the "id" field in your type isn't being treated as the
database internal _id.  The database is assigning its own internal value to
_id, which is a BSON ObjectId, which is what getSavedId() is giving you
back.

If that's not helpful, please say a little more about what were you
expecting to happen here.

Andreas Lundblad

unread,
Apr 28, 2014, 5:00:25 AM4/28/14
to mongo-jack...@googlegroups.com


On Sunday, April 27, 2014 10:50:56 PM UTC+2, Luke Palmer wrote:
Andreas,

In your example, the "id" field in your type isn't being treated as the
database internal _id.  The database is assigning its own internal value to
_id, which is a BSON ObjectId, which is what getSavedId() is giving you
back.

If that's not helpful, please say a little more about what were you
expecting to happen here.


Ok. That makes a lot of sense.

Here's what I'm trying to achieve:

1. I have some classes in a 'common' package shared by client and backend.

2. On the backend I would like to save objects of these classes in mongodb.

3. To avoid having the client depend on mongo related classes such as ObjectId, I'd like to store the object identifier as a String.

I can't figure out how to achieve this.

I've tried to add a new index of my own, and drop the "_id" index, but that doesn't seem possible. I wouldn't mind mapping my own "String id" to / from a database "ObjectId _id". Is this possible? Perhaps with a custom ObjectMapper? If yes, any tips on how to achieve this would be appreciated.

-- Andreas

Luke Palmer

unread,
Apr 28, 2014, 8:24:48 AM4/28/14
to mongo-jack...@googlegroups.com
Just use a field "String _id" and this should just work.  Keep in mind the database sets the id, not you.

Andreas Lundblad

unread,
Apr 28, 2014, 8:28:55 AM4/28/14
to mongo-jack...@googlegroups.com
On Monday, April 28, 2014 2:24:48 PM UTC+2, Luke Palmer wrote:
Just use a field "String _id" and this should just work.  Keep in mind the database sets the id, not you.


Oh gosh... That was simple.

Annotating it with @JsonProperty("_id") seems to work as well. Many thanks for your help!
Reply all
Reply to author
Forward
0 new messages