Search code examples
javaorientdb

Embedded Set,Link Set in OType in OrientDb


Can Someone give me sample code of using above mentioned types in orientdb as value to an index.I am using Document API of OrientDb-2.0.5.
What is the difference between Embedded Set and Link Set?How to choose between them?Are there any performance differences between them?


Solution

  • First, we need to know what the following mean in OrientDB (this information is available here):

    • Embedded Record: The Record is contained inside the owner. The contained Record has no RecordId
    • Link: Link to another Record

    This example will clarify it:

    private static void orientDBCollections() throws IOException {
        dropDBIfExists(URL, USER);
        createDBIfDoesNotExist(URL, USER);
    
        OPartitionedDatabasePool pool = new OPartitionedDatabasePool(URL, USER, USER);
        try (ODatabaseDocument db = pool.acquire()) {
            OSchema schema = db.getMetadata().getSchema();
            OClass classA = schema.createClass("A");
            OClass classB = schema.createClass("B");
            classA.createProperty("embMap", OType.EMBEDDEDMAP, classB);
            classA.createProperty("linkMap", OType.LINKMAP, classB);
            classA.createProperty("embSet", OType.EMBEDDEDSET, classB);
            classA.createProperty("linkSet", OType.LINKSET, classB);
    
            ODocument docA = new ODocument("A");
            ODocument docB = new ODocument("B");
            docB.field("name", "nanana");
    
            Map<String, ODocument> map = new HashMap();
            map.put("thekey", docB);
            docA.field("embMap", map, OType.EMBEDDEDMAP);
            docA.field("linkMap", map, OType.LINKMAP);
    
            Set<ODocument> set = new HashSet();
            set.add(docB);
            docA.field("embSet", set, OType.EMBEDDEDSET);
            docA.field("linkSet", set, OType.LINKSET);
    
            docA.save();
        } finally {
            pool.close();
        }
    }
    

    After this, SELECT FROM A from Studio, Console, etc... and you'll understand.