Fill the database with your data
After we did the groundwork, we are able to breathe live into our database. First, we should fill it with some basic documents we can operate with later on.
Therefore we set some documents of the classes professor, student and lectures. You can do it by following the next steps. Before each transaction, we have to call the begin()-method signalizing our database, that some operations will follow.
database.begin();
After that, new data can be uploaded.
ODocument professor_1 = new ODocument("Professor");
professor_1.field("Name", "Dagobert");
professor_1.field("Surname", "Duck");
professor_1.field("DateOfBirth","1952-05-05");
professor_1.field("Picture", "link/to/picture");
professor_1.field("Number", 12345);
// non predefined fields also possible
professor_1.field("Lecture", "Databases");
professor_1.save();
To commit our document to the database, use following piece of code also signalizing our database, that we've finished our operation.
database.commit();
If something fails, we can can simply call
database.rollback();
This aborts the transaction. All the changes will be lost.
Now we repeat it with a few more documents. It's always the same.
To set the LINKLIST, e.g. the courses taken by a student, create a list, add the references
List<OIdentifiable> linklist_student = new ArrayList<OIdentifiable>();
linklist_student.add(lecture_3);
linklist_student.add(lecture_2);
linklist_student.add(lecture_1);
linklist_student.add(lecture_5);
linklist_student.add(lecture_4);
and set it in the corresponding field of the student.
...
student_1.field("RegisteredIn", linklist_student);
...
Done with that, some operations like deletion or altering are possible. There are two ways to accomplish this. Therefore one could query the documents reference, or via a SQL command.
Deletion is a transaction. Remember this when using begin() and commit().
To delete a document, just call its delete()-- method.
database.begin();
document.delete();
database.commit();
Altering a value of a field simply use again the field() method.
document.field("key","new value");
As mentioned before, you could also use SQL commands.
Altering:
String cmd = "update "+doc.getIdentity().toString()+" add yourfield = #"+val
int ret = database.command(new OCommandSQL(cmd)).execute();
Deletion of a field:
String cmd = "update "+doc.getIdentity().toString()+" remove yourfield = #"+m_val
int ret = m_aq_db.command(new OCommandSQL(cmd)).execute();
After creation, deletion and altering, force the databases schema to update. If the command modifies the schema (like create/alter/drop class and create/alter/drop property commands), remember to force updating of the schema of the database instance you're using:
database.getMetadata().getSchema().reload();
As mentioned, you either can directly use the document, or query it via SQL commands.
For further information, all used classes in this chapter are listed below: