Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: Can't serialize an embedded object

  1. #1
    Join Date
    Apr 2010
    Posts
    24

    Default Can't serialize an embedded object

    Hi there, I'm having a problem saving an object in MongoDB. I'm using Spring Data MongoDB 1.0.0.M2

    Code:
    @Document
    public class Session {
    
    @Id private String id; private String name; private Location lastKnownLocation; private List<Location> locations = new ArrayList<Location>(); private Date lastUpdated; getters..setters
    } public class Location implements Comparable<Location>{
    private Float latitude; private Float longitude; private Date trackedOn; getters---setters
    }
    the method in the DAO where it fails is:

    Code:
    public void setLatestLocationTo(String sessionId, Location location) {
    
    WriteResult result = template.updateFirst("sessions",new Query(whereId().is(sessionId)), new Update().set("lastKnownLocation", location)); logger.info(result.toString());
    }
    I'm getting the following exception:

    Code:
    java.lang.IllegalArgumentException: can't serialize class com.x.core.beans.Location
            at org.bson.BSONEncoder._putObjectField(BSONEncoder.java:205)
            at org.bson.BSONEncoder.putMap(BSONEncoder.java:245)
            at org.bson.BSONEncoder._putObjectField(BSONEncoder.java:177)
            at org.bson.BSONEncoder.putObject(BSONEncoder.java:121)
            at org.bson.BSONEncoder.putObject(BSONEncoder.java:67)
            at com.mongodb.DBApiLayer$MyCollection.update(DBApiLayer.java:297)
            at com.mongodb.DBCollection.update(DBCollection.java:125)
            at com.mongodb.DBCollection.update(DBCollection.java:132)
            at org.springframework.data.document.mongodb.MongoTemplate$12.doInCollection(MongoTemplate.java:821)
            at org.springframework.data.document.mongodb.MongoTemplate$12.doInCollection(MongoTemplate.java:817)
            at org.springframework.data.document.mongodb.MongoTemplate.execute(MongoTemplate.java:315)
            at org.springframework.data.document.mongodb.MongoTemplate.updateFirst(MongoTemplate.java:817)
            at com.x.core.dao.MongoDBSessionsDao.setLatestLocationTo(MongoDBSessionsDao.java:75)
    And this is the template and Mongo Spring config:

    Code:
    	<bean id="mongoTemplate" class="org.springframework.data.document.mongodb.MongoTemplate">
    		<constructor-arg ref="mongo" />
    		<constructor-arg name="databaseName" value="#{serviceProperties['db']}" />
    	</bean>
    
    	<mongo:mongo host="localhost"/>
    	
    	<util:map id="serviceProperties" >
    		<entry key="db" value="mydb"/>
    	</util:map>
    So it seems that Spring can't convert Location, which is an embedded object of Session, to JSON. I've read in the SpringData Mongo Reference doc, that all JSON marshalling is done automatically? But i've also read that you have to provide converters, which actually confuses me.

    Does anyone have any idea? What can I do?

    Thanks for your time!

    Fede.

  2. #2
    Join Date
    Apr 2006
    Location
    Dresden, Germany
    Posts
    493

    Default

    A quick hint: you have to use MappingMongoConverter (through <mongo:mapping-converter />) and configure that on the MongoTemplate to have the annotations considered. Could you please try that as a first step and see if that solves the issue?

  3. #3
    Join Date
    Apr 2011
    Posts
    7

    Default

    Hi Ollie/Chiwi

    I converted my app to use the MappingMongoConverter rather than the SimpleMongoConvertor to solve another problem ( @Transient annotations) and now I have this problem with embedded objects that I didn't have before.

    Derek

  4. #4
    Join Date
    Jul 2006
    Location
    Lamar, Missouri USA
    Posts
    36

    Default

    Looks like this is a bug in the way the Update class creates a DBObject to submit to the database. In the meantime, were you to load the Session object individually and update it directly (rather than using updateFirst), I suspect it will work.
    Jon Brisbin
    SpringSource
    http://www.springsource.com

  5. #5
    Join Date
    Apr 2011
    Posts
    7

    Default

    Hi Jon

    In my situation, it would appear that the object is saved correctly in the database. And it's when I try to retrieve it that the error happens.

    Anyway to confirm this?


    Derek

    PS: Fede (sorry to hijack your thread, I hope this is useful to you too)

  6. #6
    Join Date
    Aug 2004
    Location
    New York, NY
    Posts
    74

    Default

    Hi Derek,

    Are you using SimpleMongoConverter or MappingMongoConverter. If you can post the relevant bits of config/code that would help.

    Issue https://jira.springsource.org/browse/DATADOC-114 was created to address the first issue identified in this thread (updateFirst)

    Thanks for the feedback.

    Mark

  7. #7
    Join Date
    Apr 2010
    Posts
    24

    Default

    hey there, I'll try the mongoConverter tonight when I get home. I'll keep you posted.

    Thanks for your replies!

    fede.

  8. #8
    Join Date
    Apr 2010
    Posts
    24

    Default

    Quote Originally Posted by jbrisbin View Post
    Looks like this is a bug in the way the Update class creates a DBObject to submit to the database. In the meantime, were you to load the Session object individually and update it directly (rather than using updateFirst), I suspect it will work.
    I'll try loading it and then saving it. How ever i'm concerned about the concurrency since it'd not be atomic. (the object could be modified while i pull it, change it and store it )

  9. #9
    Join Date
    Apr 2010
    Posts
    24

    Default

    Quote Originally Posted by chiwi View Post
    hey there, I'll try the mongoConverter tonight when I get home. I'll keep you posted.

    Thanks for your replies!

    fede.
    Hey Mark, you were right. It seems to be a bug since updateFirst doesn't work.
    I'll keep an eye on the bug url.

    Thanks!

    Fede.

  10. #10
    Join Date
    Apr 2010
    Posts
    24

    Default

    Quote Originally Posted by chiwi View Post
    I'll try loading it and then saving it. How ever i'm concerned about the concurrency since it'd not be atomic. (the object could be modified while i pull it, change it and store it )
    Jbrisbin, this approach works - thanks.

    However as I said. This would be only a workaround since I'm not comfortable with the idea of not being atomic.

    Thank you all.

    Fede.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •