Announcement Announcement Module
Collapse
No announcement yet.
Can't serialize an embedded object Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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?

    Comment


    • #3
      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

      Comment


      • #4
        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.

        Comment


        • #5
          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)

          Comment


          • #6
            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

            Comment


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

              Thanks for your replies!

              fede.

              Comment


              • #8
                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 )

                Comment


                • #9
                  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.

                  Comment


                  • #10
                    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.

                    Comment


                    • #11
                      Hi,
                      I'm facing the same problem, trying to save a a new object with an embbed enum and using a simple repository :

                      Code:
                      @Document(collection="adjective")
                      public class Adjective {
                      	@Id
                      	@NotNull
                      	private String id ;
                      	@NotNull
                      	private AdjectiveType type ;
                      
                      //getter setter...
                      }
                      
                      public enum AdjectiveType {
                      	POSITIVE,
                      	NEGATIVE;
                      }
                      Code:
                      public interface AdjectiveRepository extends MongoRepository<Adjective, String> {
                      }
                      I get the same error doing the following:
                      Code:
                      	...
                      	adjectiveRepository.save(myNewAdjective);
                      Any workaround ?
                      Thanks for your time

                      Comment


                      • #12
                        which converter are you using?

                        make sure you are using MappingMongoConverter


                        Fede.

                        Comment


                        • #13
                          What version are you using? There was a bug [1] we fixed recently. Please try a recent snapshot version.

                          [1] https://jira.springsource.org/browse/DATADOC-136

                          Comment


                          • #14
                            Thanks for the answer, I didn't saw this issue.
                            I was using version 1.0.0.M2.
                            I'm now trying with 1.0.0.BUILD-SNAPSHOT, but i'm probably missing something in my configuration with the new version since I get an error when the server is loading : "No bean named 'mongoDbFactory' is defined".
                            I'll try to look deeper tomorrow and let you know how it goes.

                            Quentin

                            Comment


                            • #15
                              check this thread: http://forum.springsource.org/showth...build-snapshot

                              Comment

                              Working...
                              X