Issue 377 in morphia: @Serialized doesn't work with runtime values of primitive types

15 views
Skip to first unread message

mor...@googlecode.com

unread,
Feb 10, 2012, 12:55:07 PM2/10/12
to morphia...@googlegroups.com
Status: New
Owner: ----
Labels: Type-Defect Priority-Low

New issue 377 by sylvain.wallez: @Serialized doesn't work with runtime
values of primitive types
http://code.google.com/p/morphia/issues/detail?id=377

morphia-0.99 / driver 2.7.2 / mongo 2.0.1

There's an issue with @Serialized when used on a field of type Object and
the actual value is of primitive type (Integer, Long, etc).

@Entity
public class TestClass {
@Serialized
Object data;
}

If e.g. data = new Integer(5) it is saved as an integer property. When the
object is read from the database, Morphia fails since it expects the
property to be a byte[].

This problem happens because DefaultConverters.getEncoder(Object val,
MappedField mf) looks up first in the default typed converters *before*
searching the untyped converters where the SerializedObjectConverter
resides.

Stacktrace:

Caused by: java.lang.RuntimeException:
com.google.code.morphia.mapping.MappingException: The stored data is not a
DBBinary or byte[] instance for TestClass.data ; it is a java.lang.Integer
at
com.google.code.morphia.mapping.ValueMapper.fromDBObject(ValueMapper.java:27)
at com.google.code.morphia.mapping.Mapper.readMappedField(Mapper.java:497)
at com.google.code.morphia.mapping.Mapper.fromDb(Mapper.java:480)
... 14 more
Caused by: com.google.code.morphia.mapping.MappingException: The stored
data is not a DBBinary or byte[] instance for TestClass.data ; it is a
java.lang.Integer
at
com.google.code.morphia.converters.SerializedObjectConverter.decode(SerializedObjectConverter.java:33)
at
com.google.code.morphia.converters.DefaultConverters.fromDBObject(DefaultConverters.java:129)
at
com.google.code.morphia.mapping.ValueMapper.fromDBObject(ValueMapper.java:25)
... 16 more


mor...@googlecode.com

unread,
Feb 23, 2012, 1:17:09 AM2/23/12
to morphia...@googlegroups.com
Updates:
Labels: Milestone-Icebox

Comment #1 on issue 377 by scotthernandez: @Serialized doesn't work with

(No comment was entered for this change.)

mor...@googlecode.com

unread,
Oct 10, 2013, 3:44:03 AM10/10/13
to morphia...@googlegroups.com

Comment #2 on issue 377 by peter.fu...@gmail.com: @Serialized doesn't work
with runtime values of primitive types
http://code.google.com/p/morphia/issues/detail?id=377

Following workaround seems to work:
* Wrap the @Serialized field of type Object into another class annotating
with @Embedded.
* Always use this class instead of the @Serialized field. This means: When
you want to update the @Serialized field you have to update (replace) the
whole embedded object!

--
You received this message because this project is configured to send all
issue notifications to this address.
You may adjust your notification preferences at:
https://code.google.com/hosting/settings

mor...@googlecode.com

unread,
Nov 22, 2013, 4:46:15 AM11/22/13
to morphia...@googlegroups.com

Comment #3 on issue 377 by peter.fu...@gmail.com: @Serialized doesn't work
with runtime values of primitive types
http://code.google.com/p/morphia/issues/detail?id=377

There seems not much activity on this issue but I would really be happy if
there would be a fix for that.
Is there a technical / conceptual reasons why not changing this behaviour?

If it is OK I could look into that and (hopefully) solve...
Reply all
Reply to author
Forward
0 new messages