Announcement Announcement Module
Collapse
No announcement yet.
MongoDB - How to create objects refering to each other properly? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • MongoDB - How to create objects refering to each other properly?

    Roo: 1.2.0.M1 [rev 1fa252f]
    MongoDB: 2.0.1


    I have two entities, Dummy and Dummy2.
    They refer to each other.

    Code:
    public class Dummy {
    	private String dummyString;
    	@DBRef
    	private Dummy2 dummy2;
    }
    Code:
    public class Dummy2 {
    	private String dummyString;
    	private ObjectId dummyId;
    }

    Now I try to create them both referring to each other.

    Code:
    @Test
    public void testDummyWithDummy2() {
    	DummyDataOnDemand dod = new DummyDataOnDemand();
    	Dummy dummy = dod.getNewTransientDummy(0);
    	dummyService.saveDummy(dummy);
    	
    	Dummy2DataOnDemand dod2 = new Dummy2DataOnDemand();
    	Dummy2 dummy2 = dod2.getNewTransientDummy2(0);
    	dummy2.setDummyId(dummy.getId());
    	dummy2Service.saveDummy2(dummy2);
    	
    	dummy.setDummy2(dummy2);
    	dummyService.updateDummy(dummy);
    }
    I found that the results are inconsistencies.
    There are two situations so far.

    (1) It works correctly. They refer to each other as expected.

    (2) Dummy refers to nothing and dummy2 refers to invalid ObjectId, as listed below.

    Dummy
    Code:
    {
       "_id": ObjectId("4ecfa039941836a18fe88b24"),
       "_class": "com.mytest.model.Dummy",
       "dummyString": "dummyString_2147483647" 
    }
    Dummy2
    Code:
    {
       "_id": ObjectId("4ecfa039941836a18fe88b23"),
       "_class": "com.mytest.model.Dummy2",
       "dummyString": "dummyString_0",
       "dummyId": ObjectId("4ecfa039941836a18fe88b22") 
    }

    What I did wrong? I think there are some issues that I have to concern when creating many objects together like this. Thanks!
Working...
X