google-code-export / morphia

Automatically exported from code.google.com/p/morphia
1 stars 0 forks source link

Support for @Property for list of embedded objects #365

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What version are you using? (Morphia/Driver/MongoDB)
0.99/2.7.2/1.8.2

Currently if you annotate a List of embedded objects (let's say type 
com.package.Foo) with a Property annotation, you get an exception:
java.lang.IllegalArgumentException: can't serialize class com.package.Foo
    at org.bson.BasicBSONEncoder._putObjectField(BasicBSONEncoder.java:234)
    at org.bson.BasicBSONEncoder.putIterable(BasicBSONEncoder.java:259)
    at org.bson.BasicBSONEncoder._putObjectField(BasicBSONEncoder.java:198)
    at org.bson.BasicBSONEncoder.putObject(BasicBSONEncoder.java:140)
    at org.bson.BasicBSONEncoder.putObject(BasicBSONEncoder.java:86)
    at com.mongodb.DefaultDBEncoder.writeObject(DefaultDBEncoder.java:27)
    at com.mongodb.OutMessage.putObject(OutMessage.java:142)
    at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:252)
    at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:211)
    at com.mongodb.DBCollection.insert(DBCollection.java:57)
    at com.mongodb.DBCollection.insert(DBCollection.java:87)
    at com.mongodb.DBCollection.save(DBCollection.java:716)
    at com.google.code.morphia.DatastoreImpl.save(DatastoreImpl.java:731)
    at com.google.code.morphia.DatastoreImpl.save(DatastoreImpl.java:793)
    at com.google.code.morphia.DatastoreImpl.save(DatastoreImpl.java:787)
        at <my code>

Code of the main class looks like:
{{{
@Entity(value = "myCollection", noClassnameStored = true, slaveOk = true)

public class Foo {
    @Id
    private ObjectId id;

    @Property("newFooElements")
    private List<FooElement> fooElements;
}
}}}

FooElement class is annotated with @Embedded.

The reason for it is simple: whenever a field contains a @Property annotation, 
a ValueMapper is used which is not able to convert list elements to DBObjects.

Can you please confirm that this is a bug, and if yes, are there plans to fix it

Thanks!

Original issue reported on code.google.com by vyacheslav.zholudev on 20 Dec 2011 at 12:28

GoogleCodeExporter commented 9 years ago
In this use-case you want to use @ Embedded("newFooElements") and not @Property 
-- which indicates a simple value type not comlpex.

I will create a new annotation just for naming so this isn't so confusing.

Original comment by scotthernandez on 6 Feb 2012 at 2:18