Announcement Announcement Module
Collapse
No announcement yet.
Using an Enum in a where clause in mongodb Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Using an Enum in a where clause in mongodb

    Hi There I am trying to use an enum as a match value in a where clause for mongodb with spring data like this:

    Code:
    public List<HitCount> findPopular(EntityType entityType, int limit) {
    		
    		Query q = query(where("entityType").is(entityType)).limit(limit);
    		q.sort().on("hits", Order.DESCENDING);
    		
    		return mongoTemplate.find(q, HitCount.class);
    	}
    where EnityType is an enum.

    My basic config is as follows so i am using the mapping converter:
    Code:
    <mongo:mongo host="127.0.0.1" port="27017" />
    	<mongo:db-factory dbname="${dbName}" mongo-ref="mongo"/>
    	<mongo:repositories base-package="com.excira" />
    	
    	<mongo:mapping-converter base-package="com.excira" id="mappingConverter"/>
    	
    	<bean id="mongoTemplate" class="com.excira.ute.dao.mongo.UTEMongoTemplate">
    	  <constructor-arg ref="mongoDbFactory"/>
       	  <constructor-arg ref="mappingConverter"/>
    		<!-- property name="username" value="${userName}" / -->
    		<!-- property name="password" value="${password}" / -->
    	</bean>
    I have 2 problems as described below, any help would be appreciated.

    1) if debug is enabled, i get an error when the template tries to print out the query in a debug string saying it cant serialize the entity. Looks like it could call name() on the enum here?

    java.lang.RuntimeException: json can't serialize type : class com.excira.muchenough.MEEntityTypes
    at com.mongodb.util.JSON.serialize(JSON.java:202)
    at com.mongodb.util.JSON.serialize(JSON.java:109)
    at com.mongodb.util.JSON.serialize(JSON.java:28)
    at com.mongodb.BasicDBObject.toString(BasicDBObject.j ava:75)
    at java.lang.String.valueOf(String.java:2826)
    at java.lang.StringBuilder.append(StringBuilder.java: 115)
    at org.springframework.data.document.mongodb.MongoTem plate.doFind(MongoTemplate.java:1182)
    at org.springframework.data.document.mongodb.MongoTem plate.find(MongoTemplate.java:587)
    at org.springframework.data.document.mongodb.MongoTem plate.find(MongoTemplate.java:557)
    at com.excira.ute.stats.dao.HitCountRepositoryImpl.fi ndPopular(HitCountRepositoryImpl.java:36)



    2) If i disable debug in the log4j.properties, then while the query is being executed i get the exception below:

    java.lang.IllegalArgumentException: can't serialize class com.excira.muchenough.MEEntityTypes
    at org.bson.BSONEncoder._putObjectField(BSONEncoder.j ava:205)
    at org.bson.BSONEncoder.putObject(BSONEncoder.java:12 1)
    at org.bson.BSONEncoder._putObjectField(BSONEncoder.j ava:171)
    at org.bson.BSONEncoder.putObject(BSONEncoder.java:12 1)
    at org.bson.BSONEncoder.putObject(BSONEncoder.java:67 )
    at com.mongodb.OutMessage._appendQuery(OutMessage.jav a:62)
    at com.mongodb.OutMessage.query(OutMessage.java:39)
    at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLa yer.java:264)
    at com.mongodb.DBCursor._check(DBCursor.java:309)
    at com.mongodb.DBCursor._hasNext(DBCursor.java:431)
    at com.mongodb.DBCursor.hasNext(DBCursor.java:456)
    at org.springframework.data.document.mongodb.MongoTem plate.executeFindMultiInternal(MongoTemplate.java: 1425)
    at org.springframework.data.document.mongodb.MongoTem plate.doFind(MongoTemplate.java:1186)
    at org.springframework.data.document.mongodb.MongoTem plate.find(MongoTemplate.java:587)
    at org.springframework.data.document.mongodb.MongoTem plate.find(MongoTemplate.java:557)
    at com.excira.ute.stats.dao.HitCountRepositoryImpl.fi ndPopular(HitCountRepositoryImpl.java:36)

  • #2
    Looks like a conversion issue - there is probably no converter to handle the Enum available.

    What version of the Mongo support are you using? If you see this issue with the latest M3 version then could you file a JIRA issue here.

    Thanks.

    Comment


    • #3
      i have the same problem.. i use 1.0.0.RELEASE
      i dont think it's the converter.. it works fine in saving and retrieving the object.. it just wont work in "Where" clause.
      any ideas???

      Comment

      Working...
      X