google-code-export / morphia

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

Morphia fails to save an anonymous subclasses with a StackOverflowError #402

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
When you work with anonymous subclasses like this:

-----------------------
Order o = new Order();
List<LineItem> lines = new ArrayList<LineItem>();
lines.add(new LineItem() {{
  setLineNumber(0);
  setQuantity(2);
}});
o.setLines(lines);
orderDao.save(o);
-----------------------

Morphia fails with a StackOverflowError:

java.lang.StackOverflowError
    at java.util.LinkedList.listIterator(LinkedList.java:865)
    at java.util.AbstractList.listIterator(AbstractList.java:299)
    at java.util.AbstractSequentialList.iterator(AbstractSequentialList.java:239)
    at com.google.code.morphia.mapping.MappedClass.callGlobalInterceptors(MappedClass.java:343)
    at com.google.code.morphia.mapping.MappedClass.callLifecycleMethods(MappedClass.java:320)
    at com.google.code.morphia.mapping.Mapper.toDBObject(Mapper.java:438)
    at com.google.code.morphia.mapping.Mapper.toDBObject(Mapper.java:426)
    at com.google.code.morphia.mapping.EmbeddedMapper.toDBObject(EmbeddedMapper.java:38)
    at com.google.code.morphia.mapping.Mapper.writeMappedField(Mapper.java:534)
    at com.google.code.morphia.mapping.Mapper.toDBObject(Mapper.java:442)
    at com.google.code.morphia.mapping.Mapper.toDBObject(Mapper.java:426)
    ...

This does work:
-----------------------
Order o = new Order();
List<LineItem> lines = new ArrayList<LineItem>();
LineItem l = new LineItem();
l.setLineNumber(0);
l.setQuantity(2);
lines.add(l);
o.setLines(lines);
orderDao.save(o);
-----------------------

And also normal subclasses like this: 
class LineItemSubclass extends LineItem {...}
Work with Morphia.

I think reproducing is easy, but I can provide a working testcase if needed.

Original issue reported on code.google.com by wouter...@gmail.com on 31 Mar 2012 at 8:32

GoogleCodeExporter commented 9 years ago
Tested on
Java: jdk1.7.0_03
mongo-java-driver: 2.7.3
morphia: 0.99

Original comment by wouter...@gmail.com on 31 Mar 2012 at 8:35

GoogleCodeExporter commented 9 years ago
Yes, a test case would be great.

Original comment by scotthernandez on 3 Apr 2012 at 6:31

GoogleCodeExporter commented 9 years ago
Added a maven/eclipse testcase.

Original comment by wouter...@gmail.com on 3 Apr 2012 at 8:30

Attachments:

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
Thank you for the testcase.
Due to the double brace initialization, the LineItem receives a synthetic 
reference to the test class. The test class has a reference to the DAO object 
which results in a loop when trying to persist the object. 

I would suggest to adjust the discover method in Morphia's MappedClass class to 
exclude synthetic reference all together, not only if they are marked transient.

Original comment by sebastia...@otto.de on 20 Jul 2012 at 8:42