Enums in MongoDB cannot be serialized

Bug #789021 reported by Robert Stiller on 2011-05-27
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
Querydsl
Fix Released
Undecided
Unassigned

Bug Description

Enums cannot be serialized without transforming them.

I propose, that all java enums are serialized (and deserialized) using the name() method (or valueOf() method).

Example:

@Entity
public enum Type {
 TYPE1, TYPE2
}

public class MyObject {

 protected Type type;

 public Type getType() {
 }

 public void setType(Type type) {
  this.type = type;
 }

}

Usage:

QMyObject.myObject.type.eq(Type.TYPE1); // ends in:

java.lang.IllegalArgumentException: can't serialize class Type
 at org.bson.BSONEncoder._putObjectField(BSONEncoder.java:213)
 at org.bson.BSONEncoder.putObject(BSONEncoder.java:123)
 at org.bson.BSONEncoder._putObjectField(BSONEncoder.java:173)
 at org.bson.BSONEncoder.putObject(BSONEncoder.java:123)
 at org.bson.BSONEncoder.putObject(BSONEncoder.java:69)
 at com.mongodb.OutMessage.putObject(OutMessage.java:189)
 at com.mongodb.OutMessage._appendQuery(OutMessage.java:62)
 at com.mongodb.OutMessage.query(OutMessage.java:39)
 at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:293)
 at com.mongodb.DB.command(DB.java:152)
 at com.mongodb.DBCollection.getCount(DBCollection.java:760)
 at com.mongodb.DBCollection.getCount(DBCollection.java:731)
 at com.mongodb.DBCollection.count(DBCollection.java:697)
 at com.mysema.query.mongodb.MongodbQuery.count(MongodbQuery.java:184)
 at org.springframework.data.document.mongodb.repository.QueryDslMongoRepository.count(QueryDslMongoRepository.java:151)
 at sun.reflect.GeneratedMethodAccessor9.invoke(Unknown Source)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
 at java.lang.reflect.Method.invoke(Method.java:616)
 at org.springframework.data.repository.support.RepositoryFactorySupport$QueryExecuterMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:356)
 at org.springframework.data.repository.support.RepositoryFactorySupport$QueryExecuterMethodInterceptor.invoke(RepositoryFactorySupport.java:337)
 at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
 at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
 at $Proxy18.count(Unknown Source)

Are you using an enum as an entity? Could you explain why?

I believe enums work when used as simple properties, at least there are conversions for them available in Morphia, I am not sure about Spring Data.

Changed in querydsl:
status: New → In Progress
Robert Stiller (robert-stiller) wrote :

I've used them as simple properties of an entity.

But when it cam to a request using Predicates the exception was thrown ...

I only added the @Entity Annotation to the enum because of Morphia itself. I thought it would be needed.

But nevertheless the Exception was caused by the request using the Morphia API (Predicates) and has nothing to do with spring-data itself.

I know that spring-data supports the translation from and to enum, because I wrote the feature-request :D

I also wrote a ticket to the mongo-java-driver-team to support enums and locales, but from my point of view Morphia should be able to translate enums itself - independent from any driver ...

Fixed in SVN trunk. This was indeed a Querydsl bug.

Changed in querydsl:
status: In Progress → Fix Committed
Robert Stiller (robert-stiller) wrote :

Cool, Thank you!

Released in 2.2.0.beta5

Changed in querydsl:
status: Fix Committed → Fix Released
To post a comment you must log in.
This report contains Public information  Edit
Everyone can see this information.

Other bug subscribers