Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a recordid from OrientDB on insert?

OrientDB question...

Does anyone know how I can get the recordId after an insert:

db.save(person)

I tried below on the Person POJO:

@Id
private Object id;

but the id field was null after the save. I've googled and googled to no avail. I just need to insert an object, then get the recordid that orientdb generates.

like image 967
phil-hig Avatar asked Aug 27 '13 23:08

phil-hig


4 Answers

Define field in pojo:

@Id
private Object rid;

public Object getRid() {
    return rid;
}

When save:

YourClass proxy = db.save(yourClassInstance);
Object rid = proxy.getRid();
like image 127
zella Avatar answered Nov 14 '22 11:11

zella


I got it to work using ODocuments instead of POJOs (which works for my project). Code sample:

    ODatabaseDocumentTx db  = null;
    ODocument           doc = null;

    db = new ODatabaseDocumentTx("local:" + System.getProperty("user.home") + "/testDB");
    db.create();
    doc = new ODocument("Person");
    doc.field("name", "Peter");
    doc.save();
    String rid = doc.getIdentity().toString();
    List<ODocument> results = db.query(new OSQLSynchQuery<ODocument>("select from " + rid));
    for (ODocument aDoc : results) {
        System.out.println(aDoc.field("name"));
    }
    db.close();
like image 2
phil-hig Avatar answered Nov 14 '22 10:11

phil-hig


It's just simple here is the code:

//insertquery will be the sql statement you want to insert

    ODocument result=db.command(new OCommandSQL(insertquery)).execute();

    System.out.println(result.field("@rid"));
like image 2
ARxAR Avatar answered Nov 14 '22 10:11

ARxAR


Alternatively you can make use of getRecordByUserObject() of OObjectDatabaseTx,

OObjectDatabaseTx db = new OObjectDatabaseTx("local:" + System.getProperty("user.home") + "/testDB");

ODocument oDocument = db.getRecordByUserObject( person, true );
oDocument.save();

String rid = oDocument.getIdentity().toString(); 
like image 1
sojin Avatar answered Nov 14 '22 12:11

sojin