Uploaded image for project: 'Spring Data MongoDB'
  1. Spring Data MongoDB
  2. DATAMONGO-1037

Serialization exception in Query class with enumerations

    Details

    • Type: Bug
    • Status: Waiting for Feedback
    • Priority: Minor
    • Resolution: Unresolved
    • Affects Version/s: 1.5.2 (Dijkstra SR2)
    • Fix Version/s: None
    • Component/s: Core
    • Labels:
      None

      Description

      When enum value is used as part of Criteria object, when Query is throwing exception, serialization is failing.
      Following is stacktrace:

      java.lang.RuntimeException: json can't serialize type : class mypackage.MyClass$UserStatus
      	at com.mongodb.util.ClassMapBasedObjectSerializer.serialize(ClassMapBasedObjectSerializer.java:77)
      	at com.mongodb.util.JSONSerializers$MapSerializer.serialize(JSONSerializers.java:317)
      	at com.mongodb.util.ClassMapBasedObjectSerializer.serialize(ClassMapBasedObjectSerializer.java:79)
      	at com.mongodb.util.JSONSerializers$MapSerializer.serialize(JSONSerializers.java:317)
      	at com.mongodb.util.ClassMapBasedObjectSerializer.serialize(ClassMapBasedObjectSerializer.java:79)
      	at com.mongodb.util.JSON.serialize(JSON.java:55)
      	at com.mongodb.util.JSON.serialize(JSON.java:40)
      	at com.mongodb.BasicDBObject.toString(BasicDBObject.java:83)
      	at java.lang.String.valueOf(String.java:2854)
      	at java.lang.StringBuilder.append(StringBuilder.java:128)
      	at org.springframework.data.mongodb.core.query.Query.addCriteria(Query.java:88)

      Also on side note (may qualify for another bug which caused above exception to appear) : MongoDB allows same multiple criteria on same key, For e.g., db.business.find({status:'Active', status:{$ne:'InActive'}},

      {status:1}

      ) is working fine from mongodb console. If I try same thing using Criteria it causes exception. I can work around the problem, but in certain cases I would like to allow this where criteria is prepared dynamically.

        Activity

        Hide
        poornasubhash Poorna Subhash added a comment -

        I have wrongly put this in DATAJPA Jira. Supposed to be in DATAMONGO. Is there a way to move this to that JIRA?

        Show
        poornasubhash Poorna Subhash added a comment - I have wrongly put this in DATAJPA Jira. Supposed to be in DATAMONGO. Is there a way to move this to that JIRA?
        Hide
        olivergierke Oliver Gierke added a comment -

        Any chance you add a bit more information how you actually get to that stack trace? I can't see a toString() on a DBObject in the addCriteria(…) method of Query in version 1.5.2 actually.

        Be reminded that you cannot simply call toString(…) on a DBObject which is due to the fact that this driver type will throw exactly the exception you see if it contains non-MongoDB-primitive types. In Spring Data MongoDB, these types will be mapped later on so that the query will work out eventually. However if you need to serialize a DBObject in between, use SerializationUtils.serializeToJsonSafely(…).

        Regarding the other issue: first, lets try to stick with one issue per ticket, otherwise we get lost. Second, this is a limitation of the Java Driver, which uses a Map to build up the query and even if you held all criterias for a given key in a plain array, it wasn't clear whether you'd want to concatenate them via AND or OR. The console seems to default it to some way but with the Java driver and Spring Data MongoDB in turn you explicitly need to use Criteria.andOperator(…) or Criteria.orOperator(…) explicitly if you want to have multiple constraints for the very same key.

        Show
        olivergierke Oliver Gierke added a comment - Any chance you add a bit more information how you actually get to that stack trace? I can't see a toString() on a DBObject in the addCriteria(…) method of Query in version 1.5.2 actually. Be reminded that you cannot simply call toString(…) on a DBObject which is due to the fact that this driver type will throw exactly the exception you see if it contains non-MongoDB-primitive types. In Spring Data MongoDB, these types will be mapped later on so that the query will work out eventually. However if you need to serialize a DBObject in between, use SerializationUtils.serializeToJsonSafely(…) . Regarding the other issue: first, lets try to stick with one issue per ticket, otherwise we get lost. Second, this is a limitation of the Java Driver, which uses a Map to build up the query and even if you held all criterias for a given key in a plain array, it wasn't clear whether you'd want to concatenate them via AND or OR . The console seems to default it to some way but with the Java driver and Spring Data MongoDB in turn you explicitly need to use Criteria.andOperator(…) or Criteria.orOperator(…) explicitly if you want to have multiple constraints for the very same key .

          People

          • Assignee:
            olivergierke Oliver Gierke
            Reporter:
            poornasubhash Poorna Subhash
            Last updater:
            Oliver Gierke
          • Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

            • Created:
              Updated: