Java API - ODatabaseDocument

This class provides a unified Multi-Model interface for managing and administering OrientDB Document and Graph databases.

Managing Databases

In previous versions of OrientDB, separate classes were used for Document and Graph databases. This is because OrientDB began as a Document database and added support for Graph databases later, based on the Apache TinkerPop 2.x standard. Beginning in version 3.0.x using this class, OrientDB provides a common interface for both database types. It can be found at com.orientechnologies.orient.core.db.document . For instance,

import com.orientechnologies.orient.core.db.document.ODatabaseDocument;

Once you've imported the class to your application, you can use one of the constructors to build a particular instance in your code.

Example

In order to operate on an instance of this class, you need to create it on the OrientDB Server, then open it through an OrientDB() or OServer instance. For example, in your own application you might want to streamline the startup process, so that it creates a working database in the event that one isn't ready, then opens it.

// INITIALIZE VARIABLES
private OrientDB orientdb;

// OPEN/CREATE DATABASE
public static ODatabaseDocument openDatabase(String name) {

   // Initialize Server Connection
   orientdb = new OrientDB("embedded:/tmp/", OrientDBConfig.defaultConfig());

   // Check if Database Exists
   if (!orientdb.exists(name)){
      orientdb.create(name, ODatabaseType.PLOCAL);
   }

   return orientdb.open(name, "admin", "admin");

}

Here, the method uses the exists method to determine of the database exists, runs the create() if it doesn't, then uses open() with default credentials to open the ODatabaseDocument instance in your application.

Methods

MethodReturn TypeDescription
close()voidCloses the database
command()OResultSetExecutes idempotent or non-idempotent query
execute()OResultSetExecutes a query
getMetadata()OMetadataRetrieves the database metadata
getName()StringRetrieves the logical name of the database
getURL()StringRetrieves the database URL
getUser()OSecurityUserRetrieves the current user
incrementalBackup()StringPerforms an incremental backup of the database to the given path
isActiveOnCurrentThread()booleanChecks whether the current database is active on the current thread
isClosed()booleanChecks whether the database is closed
live()OLiveQueryMonitorSubscribes query as a live query
load()<RET extends T> RETLoads a record by its Record ID
query()OResultSetQueries the database
registerListener()voidSubscribes the given listener to database events
save()TSaves the given entity to the database
unregisterListener()voidUnsubscribes the given listener from database events

Managing Classes and Clusters

MethodReturn TypeDescription
addCluster()intAdds a cluster to the database.
browseClassORecordIteratorClass<ODocument>Retrieves all records of the given database class
browseCluster()<REC extends ORecord> ORecordIteratorCluster<REC>Retrieves all records of the given cluster
countClass()longRetrieves the number of records in the given database class
countClusterElements()longCounts all entities in the specified cluster.
createClass()OClassCreates a new database class
createClassIfNotExists()OClassCreates a new database class, if not exists
createEdgeClass()OClassCreates a database class as an extension of the E edge class
createVertexClass()OClassCreates a database class as an extension of the V vertex class
dropCluster()booleanRemoves cluster from database
existsCluster()booleanDetermines whether a cluster exists on the database
getClass()OClassRetrieves the given class from the database
getClusterIdByName()intRetrieves the Cluster ID for the given cluster name
getClusterNameById()StringRetrieves the cluster name for the given ID
getClusters()intReturns the number of clusters on the database
getDefaultClusterId()intReturns the default Cluster ID
truncateCluster()voidRemoves all data from the given cluster

Managing Records

MethodReturn TypeDescription
delete()ODatabase<T>Removes a record from the database
getRecord()<RET extends ORecord> RETRetrieves a record from the database
getRecordType()byteReturns the default record type
newBlob()OBlobCreates a new instance of a binary blob containing the given bytes
newEdge()OEdgeCreates a new edge between the given vertices
newElement()OElementCreates a new element (that is, a document, vertex or edge)
newInstance()RETCreates a new document, vertex or edge
newVertex()OVertexCreates a new vertex

Managing Transactions

MethodReturn TypeDescription
begin()ODatabase<T>Initiates a transaction
commit()ODatabase<T>Commits a transaction
getTransaction()OTransactionRetrieves the current transaction
rollback()ODatabase<T>Reverts changes

Managing Intents

MethodReturn TypeDescription
declareIntent()booleanDeclares an intent for the database.
getActiveIntent()OIntentReturns the Intent active for the current session