Create your database architecture

After setting up a raw database like mentioned before - since a document database is predestined for highly unstructured data - you could start filling your database with data without setting up any kind of index, schema or constraints.

To do so, just create a new document and fill its fields with your data.

ODocument student = new ODocument("Student");
student.field( "name", "Doe" );
student.field( "surname", "John");
student.field( "faculty", "CS" );
student.save();

Never forget to save the created document. Since without calling the save()-method, the data won't be pushed into the database.

So far, there are no kind of restrictions set. When an ODocument instance is saved, the declared type "Student" will be created without constraints. This is called schema-less mode.

In most cases, best choice is to set some kind of schema (either full or hybrid) to organize your data.

In our case, it is best to use the schema-hybrid mode.

To get access to the databases schema, get a reference of type OSchema.

OSchema schema = database.getMetadata().getSchema();

After that, you are able to get and set schemata onto your database. In our case, a superclass 'Person' will be implemented.

OClass person = schema.createClass("Person").setAbstract(true).setStrictMode(false);

As you can see, we call the setAbstract() - method to make sure it's the superclass. To enable the hybrid-mode, we further call the setStrictMode() - method with the argument false.

After that, we can work with our new abstract class by calling the getClass() - method of the schema of the database.

 OClass person = schema.getClass("Person");

If a null reference will be returned, there is no such class set up in the schema of your database. Make sure to handle this case by checking if there's a null pointer.

if(person == null)
{
  ...
}

Now that we are done with creating our class, we are able to set the constraints for our later used fields by calling the createProtery() - method. If a property is set as mandatory, the fields have to be filled with the specified OType.

person.createProperty("Name", OType.STRING).setMandatory(true).setNotNull(true);
...
person.createProperty("DateOfBirth", OType.DATE).setMandatory(true).setNotNull(true);
person.createProperty("Picture", OType.BINARY).setMandatory(true).setNotNull(true);
person.createProperty("Number", OType.INTEGER).setRegexp("[1-9]{5}").setMandatory(true).setNotNull(true);

To make sure, there will be no duplicate data, just create an Index for your special needs.

person.createIndex("PersonIdx", OClass.INDEX_TYPE.UNIQUE, "Number","Name","Surname","DateOfBirth");
person.createIndex("NumberIdx", OClass.INDEX_TYPE.UNIQUE, "Number");

In the first row, the combination of the fields "Number", "Name", "Surname" and "DateOfBirth" must be unique across all subclasses of 'Person'.

The second index ensures that the value of the field "Number" is unique by itself across the database.

After this step, we can go on by creating the need subclasses by inherit from our superclass.

OClass professor = schema.createClass("Professor").addSuperClass(person);

To set some additional properties, get the class from the schema and as done with the superclass, just append any kind of property. There are no restrictions.

OClass professor = schema.getClass("Professor");
professor.createProperty(" .... ");

Also, to get rid of a property, just drop it.

professor.dropProperty(" ... ");

After that, because only the property was being dropped, we also have to delete the entries in this property.

db.command(new OCommandSQL("UPDATE Professor REMOVE name")).execute();

Now we repeat these steps with our other classes 'Student' and 'Lecture'. 'Student' will be a subclass of 'Person', 'Lecture' obviously won't. Therefore, we have to create a non-inherited class with own properties without calling the setSuperClass()-method.

OClass lecture = schema.getClass("Lecture");
...
lecture = schema.createClass("Lecture");
lecture.createProperty("Name", OType.STRING).setMandatory(true).setNotNull(true);
lecture.createProperty("Number", OType.INTEGER).setRegexp("[1-9]{4}").setMandatory(true).setNotNull(true);
lecture.createProperty("Credits", OType.INTEGER).setMandatory(true).setNotNull(true);
lecture.createProperty("LecturedBy", OType.LINK, professor).setMandatory(true).setNotNull(true);
}

After being done creating and editing our schema, we have to save it against the database's schema handler.

schema.save();

Reminder: Changes to the schema are not transactional, so execute them outside a transaction.

By clicking on the links, you can infrom yourself about OTypes (and here) and constraints:

And more about relationships in the OrientDB document database here.

For further information, all used classes in this chapter are listed below:

results matching ""

    No results matching ""