How to get JAVA object from OResultSet

333 Views Asked by At

I have the following code. And problem is that I was unable to find any documentation on how to extract/cast OResult to my java type and get pojo.

OrientDBObject orientDBObject;
try (ODatabaseObject databaseObjectInner = orientDBObject.open(dbName,     username, password)) {
    specFromDB = getEntitySpecInt3(databaseObjectInner, objectId, rid);
    try (OResultSet resultSet = databaseObject.query(queryByRid)) {
        if (!resultSet.hasNext()) {
            return null;
        }

        Object specObj = resultSet.next();

        // how to cast properly?
        return (EntitySpec) specObj;
        }
    }

How to cast or get object from OResult?

Thanks.

1

There are 1 best solutions below

1
Luigi Dell'Aquila On BEST ANSWER

In OrientDB v 3.0 you have two ways to do it.

The easy one: just use db.objectQuery() instead of db.query(), it just returns POJOs.

The second way is to extract the OIdentifiable from the OResult and then use db.getUserObjectByRecord() to convert it to a POJO:

OResult item = resultSet.next();
OIdentifiable doc = item.toElement();
Object pojo = db.getUserObjectByRecord(doc, null);