Question

Using OrientDB 1.7-rc and Scala, I would like to insert a document (ODocument), into a document (not graph) database, with connections to other documents. How should I do this?

I've tried the following, but it seems to insert an embedded list of documents into the Package document, rather than connect the package to a set of Version documents (which is what I want):

val doc = new ODocument("Package")
  .field("id", "MyPackage")
  .field("versions", List(new ODocument("Version").field("id", "MyVersion")))

EDIT:

I've tried inserting a Package with connections to Versions through SQL, and that seems to produce the desired result:

insert into Package(id, versions) values ('MyPackage', [#10:3, #10:4] )

However, I need to be able to do this from Scala, which has yet to produce the correct results when loading the ODocument back. How can I do it (from Scala)?

Was it helpful?

Solution 3

This is how to insert a Package with a linkset referring to an arbitrary number of Versions:

val version = new ODocument("Version")
  .field("id", "1.0")
version.save()

val versions = new java.util.HashSet[ODocument]()
versions.add(version)
val package = new ODocument("Package")
  .field("id", "MyPackage")
  .field("versions", versions)
package.save()

When inserting a Java Set into an ODocument field, OrientDB understands this to mean one wants to insert a linkset, which is an unordered, unique, collection of references.

When reading the Package back out of the database, you should get hold of its Versions like this:

val versions = doc.field[java.util.HashSet[ODocument]]("versions").asScala.toSeq

As when the linkset of versions is saved, a HashSet should be used when loading the referenced ODocument instances.

Optionally, to enforce that Package.versions is in fact a linkset of Versions, you may encode this in the database schema (in SQL):

create property Package.versions linkset Version

OTHER TIPS

You need to create the individual documents first and then inter-link them using below SQL commands.

Some examples given in OrientDB documentation

insert into Profile (name, friends) values ('Luca', [#10:3, #10:4] )

OR

insert into Profile SET name = 'Luca', friends =  [#10:3, #10:4]

Check here for more details.

I tried posting in comments above, but somehow the code is not readable, so posting the response separately again.

Here is an example of linking two documents in OrientDB. This is take from documentation. Here we are adding new user in DB and connecting it to give role:

var db = orient.getDatabase();
    var role = db.query("select from ORole where name = ?", roleName); 
    if( role == null ){
      response.send(404, "Role not found", "text/plain", "Error: role name not found" );
    } else {

      db.begin();
      try{
        var result = db.save({ "@class" : "OUser", name : "Gaurav", password : "gauravpwd", roles : role});
        db.commit();
        return result;
      }catch ( err ){
        db.rollback();
        response.send(500, "Error: Server", "text/plain", err.toString() );
      }
    }

Hope it helps you and others.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top