Search code examples
orientdbnosql

Cascade save on OrientDB Document API when using embedded types


Given the following test:

    // setup
    OClass driver = getDatabase().getMetadata().getSchema().createClass(DRIVER);
    OClass car = getDatabase().getMetadata().getSchema().createClass(CAR);
    car.createProperty(DRIVERS, OType.EMBEDDEDLIST, driver);
    OClass team = getDatabase().getMetadata().getSchema().createClass(TEAM);
    team.createProperty(CARS, OType.EMBEDDEDSET, car);

    // exercise
    ODocument alonso = new ODocument(DRIVER).field("name", "Fernando Alonso").field("nationality", "Spanish")
            .field("yearOfBirth", 1981);
    ODocument button = new ODocument(DRIVER).field("name", "Jenson Button").field("nationality", "british")
            .field("yearOfBirth", 1980);
    ODocument mp30 = new ODocument(CAR).field(DRIVERS, Arrays.asList(new ODocument[] { alonso, button }));
    Set<ODocument> cars = new HashSet<>();
    cars.add(mp30);
    ODocument mclarenF1Team = new ODocument(TEAM).field(CARS, cars);

    mclarenF1Team.save();

    // verify
    assertEquals(1, getDatabase().countClass(TEAM));
    assertEquals(1, getDatabase().countClass(CAR));
    assertEquals(2, getDatabase().countClass(DRIVER));

The second assertion fails:

java.lang.AssertionError: expected:<1> but was:<0> at org.junit.Assert.fail(Assert.java:88) at org.junit.Assert.failNotEquals(Assert.java:834) at org.junit.Assert.assertEquals(Assert.java:645) at org.junit.Assert.assertEquals(Assert.java:631) at foo.orientdb.dataaccessapi.StoreJSonIT.testSchemaFull(StoreJSonIT.java:68)

Why does it fail?

The properties CAR and DRIVER are created as embedded list and embedded set, shouldn't a single save in mclarenF1Team do a cascade save for the embedded documents?


Solution

  • Embedded List/Set means that the documents you create will be embedded (Saved) in the parent document and not in his own class/cluster.

    IF you want to achieve that behaviour you should use links

    See here

    http://orientdb.com/docs/2.1/Concepts.html#relationships