How to conveniently use Object Database and Graph Database APIs together?

715 views
Skip to first unread message

Mich Geni

unread,
Jun 25, 2012, 4:31:03 AM6/25/12
to orient-...@googlegroups.com

hi,

OrientDB provides a very rich API and query language to do a variety of things we can think as a DB.

But I see a small effort needs to be done to use it in a more powerful and convenient way. e.g. use Java native classes (POJOs) as first class Documents and also Vertices and Edges of Graph.

I was doing a small test to do this. Kindly consider the code below:

            OObjectDatabaseTx db = OObjectDatabasePool.global().acquire("remote:localhost:2424/graphtest1", "admin", "admin");
            db
.getEntityManager().registerEntityClass(Profile.class);
            db
.getEntityManager().registerEntityClass(Knows.class);

           
OGraphDatabase graph = OGraphDatabasePool.global().acquire("remote:localhost:2424/graphtest1", "admin", "admin");

           
try {
                db
.begin(TXTYPE.OPTIMISTIC);

               
// One person's profile
               
Profile suan = db.newInstance(Profile.class);
                suan
.setName("Suan Jua");
                suan
.setLanguage("en");
                suan
.setEmail("su...@example.com");

                db
.save(suan);

               
// Another person's profile
               
Profile nikito = db.newInstance(Profile.class);
                nikito
.setName("Nikito Jamahona");
                nikito
.setLanguage("Japanese");
                nikito
.setEmail("nik...@example.com");

                db
.save(nikito);

               
// RID is temporary before a transaction is committed.
               
//System.out.println("RID: " + db.getRecordByUserObject(suan, false).getIdentity().toString());
               
                db
.commit();
               
               
// RID is actual after a transaction is committed.
                
System.out.println("RID: " + db.getRecordByUserObject(suan, false).getIdentity().toString());
                System.out.println(db.getRecordByUserObject(nikito, false).toJSON());

                graph
.begin(TXTYPE.OPTIMISTIC);

               
//graph.createEdgeType("Knows");
               
ODocument e = graph.createEdge(db.getRecordByUserObject(suan, false).getIdentity(),
                        db
.getRecordByUserObject(nikito, false).getIdentity(), "Knows");
                e
= e.save();
               
                graph
.commit();
               
                
System.out.println("-----------: " + e.getIdentity());

               
// Method 1:
               
List<Knows> suanKnowsnikitoList = db.query(new OSQLSynchQuery<Knows>("select from " + e.getIdentity().toString()));
               
               
Knows suanKnowsnikito = suanKnowsnikitoList.get(0);

                suanKnowsnikito
.setKnowHow("peers");

               
Calendar cal = Calendar.getInstance();
                cal
.set(Integer.parseInt("2010"), Integer.parseInt("06"), Integer.parseInt("21"));
                suanKnowsnikito
.setSince(cal.getTime());

                db
.save(suanKnowsnikito);
               
                System.out.println("-----------: pojo= " + suanKnowsnikito.getKnowHow());

               
// Method 2:
               
Object o = db.getUserObjectByRecord(e, null);
               
               
Knows k = (Knows)o;

                k
.setKnowHow("friend");

                db
.save(k); // Without a transaction.

               
System.out.println("-----------: pojo= " + k.getKnowHow());
               
           
} catch (Exception e) {
                e
.printStackTrace();
                db
.rollback();
           
} finally {
                db
.close();
                graph
.close();
           
}



NOTE:
  • Many of the Exceptions have been ignored for easiness
  • The graph.rollback was ignored for simplicity (as it is in the same try-catch-finally block as the db)

My questions are:

  1. I want to use both OObjectDatabaseTx and OGraphDatabase together and interchangeably. Any recommended and convenient practices for this? Do we need some additional convenience API in core, for using them together?
  2. Is there a way that the connection created by OObjectDatabaseTx is also used by OGraphDatabase? Or the connection thread is provided from a global pool, shared by both classes (in which case opening a connection in instantiating both of them does not create a new connection for each)?
  3. Is there a method to do all the operations in a single transaction, instead of two? (Currently a OGraphVertex object Person is created twice in DB in one transaction, then in another an OGraphEdge object Knows is created and links of both Persons are updated in the second transaction) (SideNote: I did the inheritance of  OGraphVertex -> Person at DB level not in Java code. Since by default the OGraphDatabase inserts everything in OGraphVertex object in DB. To prevent this and store every POJO in its own class, I had to inherit every POJO class from OGraphVertex in DB)
  4. How to convert ODocument (returned by most of the methods of  OGraphDatabase) to POJOs (e.g. Knows in this specific case) and vice versa?
  5. The statement Object o = db.getUserObjectByRecord(e, null);  gives runtime error: java.lang.NoSuchMethodError: com.orientechnologies.orient.object.db.OObjectDatabaseTx.getUserObjectByRecord(Lcom/orientechnologies/orient/core/db/record/OIdentifiable;Ljava/lang/String;)Ljava/lang/Object;   What am I doing wrong here?
  6. I want to avoid the "Method 1" in favor of "Method 2". But I can't do it because of point #5 above.
  7. What are the best practices you would like to share about the code here and in implementing its intent? Which things can be cut to short as convenient and efficient? I hope this will be beneficial for many people here.
The list of questions is a bit long. But I am optimistic, that it will answer a good set of common questions in this regard.

Regards

Mich Geni

unread,
Jun 25, 2012, 5:04:16 AM6/25/12
to orient-...@googlegroups.com

Sorry, I didn't mention version. I am using svn trunk r5926.


On Monday, June 25, 2012 1:31:03 PM UTC+5, Mich Geni wrote:

hi,

OrientDB provides a very rich API and query language to do a variety of things we can think as a DB.

But I see a small effort needs to be done to use it in a more powerful and convenient way. e.g. use Java native classes (POJOs) as first class Documents and also Vertices and Edges of Graph.

I was doing a small test to do this. Kindly consider the code below:

            OObjectDatabaseTx db = OObjectDatabasePool.global().acquire("remote:localhost:2424/graphtest1", "admin", "admin");
            db
.getEntityManager().registerEntityClass(Profile.class);
            db
.getEntityManager().registerEntityClass(Knows.class);

           
OGraphDatabase graph = OGraphDatabasePool.global().acquire("remote:localhost:2424/graphtest1", "admin", "admin");

           
try {
                db
.begin(TXTYPE.OPTIMISTIC);

               
// One person's profile
               
Profile suan = db.newInstance(Profile.class);
                suan
.setName("Suan Jua");
                suan
.setLanguage("en");

                suan
.setEmail("suan@example.com");


                db
.save(suan);

               
// Another person's profile
               
Profile nikito = db.newInstance(Profile.class);
                nikito
.setName("Nikito Jamahona");
                nikito
.setLanguage("Japanese");

                nikito
.setEmail("nikito@example.com");

Mich Geni

unread,
Jun 26, 2012, 2:38:04 PM6/26/12
to orient-...@googlegroups.com

Or put it more simple:

Can we add Object (POJOs) mapping to Graph DB (OGraphDatabase) API? 

The OGraphDatabase is fast and light. Just adding one more layer on top of it (like Frames on top of Gremlin and Blueprints) for object mapping and manipulation in native way, will make things much convenient.

For example, the blind way as used by OGraphDatabase.createVertex().field() can be taken over by Java types and business logic constraints etc.

And that a Course -> assigned-to -> Student kind of things can be done conveniently.


Regards.

Luca Garulli

unread,
Jun 30, 2012, 7:40:22 PM6/30/12
to orient-...@googlegroups.com
Hi,
you could create your vertex and edge POJOs and this is what we did 1 years ago but today that API is deprecated because:
  • users need speed avoiding the beauty of having just some POJOs
  • Blueprints already provides such similar API
Lvc@

--
 
 
 

Ranqi Zhu

unread,
May 22, 2013, 1:17:15 AM5/22/13
to orient-...@googlegroups.com
What is Blueprints' similar API?

                suan
.setEmail("su...@example.com");


                db
.save(suan);

               
// Another person's profile
               
Profile nikito = db.newInstance(Profile.class);
                nikito
.setName("Nikito Jamahona");
                nikito
.setLanguage("Japanese");

                nikito
.setEmail("nik...@example.com");

--
 
 
 

Luca Garulli

unread,
May 22, 2013, 4:34:38 AM5/22/13
to orient-database


--
 
---
You received this message because you are subscribed to the Google Groups "OrientDB" group.
To unsubscribe from this group and stop receiving emails from it, send an email to orient-databa...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

social...@gmail.com

unread,
Jan 1, 2016, 4:10:49 AM1/1/16
to OrientDB

Hi

How can I save my pojo class as vertex in Graph API. (I know, how to do in object and from command operations) looking only from Graph API.

public class Customer implements Serializable {

 

    @OId

    private String rid;

   

    @OVersion

    private Long version;

   

    private String firstName;

 

    private String lastName;

}

I want how directly I can save this pojo ?

I know this also.

OrientGraph graph = new OrientGraph("remote:localhost/test", "admin", "admin");

graph.createVertexType("Customer");

Vertex cust = graph.addVertex("class:Customer");

            cust.setProperty("firstName", "John");

            cust.setProperty("lastName", "Smith");

 

is there any API that directly save pojo as vertex. Pls let me know

Reply all
Reply to author
Forward
0 new messages