Created
July 5, 2012 01:54
-
-
Save greenlaw110/3050516 to your computer and use it in GitHub Desktop.
Morphia Customer decode method not called
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
play.exceptions.JavaExecutionException: java.lang.RuntimeException: java.lang.ClassCastException: java.lang.String cannot be cast to com.mongodb.DBObject | |
at play.mvc.ActionInvoker.invoke(ActionInvoker.java:237) | |
at Invocation.HTTP Request(Play!) | |
Caused by: java.lang.RuntimeException: java.lang.RuntimeException: java.lang.ClassCastException: java.lang.String cannot be cast to com.mongodb.DBObject | |
at com.google.code.morphia.mapping.Mapper.fromDb(Mapper.java:487) | |
at com.google.code.morphia.mapping.Mapper.fromDBObject(Mapper.java:267) | |
at com.google.code.morphia.query.MorphiaIterator.convertItem(MorphiaIterator.java:66) | |
at com.google.code.morphia.query.MorphiaIterator.processItem(MorphiaIterator.java:53) | |
at com.google.code.morphia.query.MorphiaIterator.next(MorphiaIterator.java:48) | |
at com.google.code.morphia.query.QueryImpl.get(QueryImpl.java:365) | |
at play.modules.morphia.Model$MorphiaQuery.get(Model.java:1400) | |
at controllers.Test.test4(Test.java:21) | |
at play.mvc.ActionInvoker.invokeWithContinuation(ActionInvoker.java:557) | |
at play.mvc.ActionInvoker.invoke(ActionInvoker.java:508) | |
at play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:484) | |
at play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:479) | |
at play.mvc.ActionInvoker.invoke(ActionInvoker.java:161) | |
... 1 more | |
Caused by: java.lang.RuntimeException: java.lang.ClassCastException: java.lang.String cannot be cast to com.mongodb.DBObject | |
at com.google.code.morphia.mapping.EmbeddedMapper.fromDBObject(EmbeddedMapper.java:146) | |
at com.google.code.morphia.mapping.Mapper.readMappedField(Mapper.java:507) | |
at com.google.code.morphia.mapping.Mapper.fromDb(Mapper.java:484) | |
... 13 more | |
Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to com.mongodb.DBObject | |
at com.google.code.morphia.mapping.EmbeddedMapper.fromDBObject(EmbeddedMapper.java:136) | |
... 15 more |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Entity | |
@Converters({PeriodConverter.class}) | |
public class MyModel { | |
public Period period = Period.all; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public enum Period { | |
day("s"), week("w"), month("m"), all("a"); | |
private String shortName; | |
private Period(){} | |
private Period(String s) { | |
shortName = s; | |
} | |
public String getShortName() { | |
return shortName; | |
} | |
public String shortName() { | |
return shortName; | |
} | |
public static Period parseStr(String s) { | |
for (Period p: Period.values()) { | |
if (p.name().equals(s)) return p; | |
else if (p.getShortName().equals(s)) return p; | |
} | |
throw new IllegalArgumentException("No enum const Period." + s); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class PeriodConverter extends TypeConverter { | |
public PeriodConverter() { | |
super(com.pixolut.pxtrack.prl.Period.class); | |
} | |
@Override | |
public Object decode(Class targetClass, Object fromDBObject, MappedField optionalExtraInfo) throws MappingException { | |
// This method never get called! | |
return com.pixolut.pxtrack.prl.Period.parseStr((String) fromDBObject); | |
} | |
@Override | |
public Object encode(Object value, MappedField optionalExtraInfo) { | |
return ((com.pixolut.pxtrack.prl.Period)value).shortName(); | |
} | |
} |
Me too :(
It's janky, but I was able to get it working when my TypeConverter implemented SimpleValueConverter (even though I wasn't doing a simple value conversion). Give it a go!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Run into the same problem. Did you ever find a solution to the problem?