Open GoogleCodeExporter opened 9 years ago
Maybe this should be handled by ConvertableTypeTranslator?
Original comment by mar...@probst.io
on 6 Jun 2011 at 10:58
The problem is that it is not possible to embed a primitive type as they do not
have fields. I think you are probably just needing to remove the @Embedded
annotation and the default translator will first try to encode as a native type.
Is this what you are after?
Original comment by j...@citikey.com
on 6 Jun 2011 at 11:34
Possibly, but I actually wanted to mix primitive and non-primitive values in
that particular map (that's why I have the polymorphic attribute). I assume I
can wrap the primitives in another class to work around this.
The NPE is probably not intended behaviour, is it?
Original comment by mar...@probst.io
on 6 Jun 2011 at 11:39
Ok, I've created this test case that shows your situation working fine. Modify
it to make it fail if you are doing something different
package com.google.code.twig.tests.issues;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import org.junit.Test;
import com.google.code.twig.LocalDatastoreTestCase;
import com.google.code.twig.annotation.AnnotationObjectDatastore;
import com.google.code.twig.annotation.Embedded;
import com.google.code.twig.annotation.Id;
public class Issue66Test extends LocalDatastoreTestCase
{
static class Bag
{
@Id Long id;
@Embedded(polymorphic = true)
HashMap<String, Object> attrs = new HashMap<String, Object>();
}
static class Stuff
{
String name = "j-lo";
String asset = "ass";
}
@Test
public void mixedPrimitiveAndObjectMap()
{
AnnotationObjectDatastore datastore = new AnnotationObjectDatastore();
Bag bag = new Bag();
bag.attrs.put("bar", new Date());
bag.attrs.put("num", 8);
bag.attrs.put("stuff", new Stuff());
datastore.store(bag);
datastore.disassociateAll();
List<Bag> bags = datastore.find().type(Bag.class).returnAll().now();
assert bags.size() == 1;
}
}
Original comment by j...@citikey.com
on 6 Jun 2011 at 12:13
Strange, the code from trunk inserts a ConfigurationFieldTranslator into the
chain that does not end up there when I run from my local project.
But that translator isn't either, it silently deserializes the primitive values
into instances of Object that don't contain any data.
@Test
public void mixedPrimitiveAndObjectMap()
{
AnnotationObjectDatastore datastore = new AnnotationObjectDatastore();
Bag bag = new Bag();
Date dateValue = new Date();
bag.attrs.put("bar", dateValue);
bag.attrs.put("num", 8);
bag.attrs.put("stuff", new Stuff());
datastore.store(bag);
datastore.disassociateAll();
List<Bag> bags = datastore.find().type(Bag.class).returnAll().now();
assert bags.size() == 1;
assertEquals(dateValue, bags.get(0).attrs.get("bar"));
}
Above test will fail, same for "num". I'll investigate some more to see if the
new translator only got introduced at trunk.
Original comment by mar...@probst.io
on 6 Jun 2011 at 2:00
Original issue reported on code.google.com by
martinpr...@google.com
on 6 Jun 2011 at 10:54