Sunday, July 10, 2011

Usage of DocumentReferences in ektorp for CouchDB

We are using ektorp as a java API for CouchDB and had a scenario of parent/child documents, though document based databases should not be used in the cases were there is a need for relational data, in practical world we end up having some sort of relationships between multiple documents (eg: parent/child etc). The primary reason being the choice of the document based database itself.
People go for databases like CouchDB not because there is a need for schema less data but for the other cool features these databases gives us like replication (master to master), conflict resolution etc., which can be leveraged to have a offline capability for applications. I think this itself is the smell, features like these should not drive one to use NoSQL databases, because the application domain needs the relational data.
Any ways the choice is made (yet to be challenged) and we had to figure out how to model our documents and persist in the database which has the parent/child relationship. CouchDB itself does not care how we store documents its up to the API to manage these relationships. Ektorp provides a way to achieve this (however its bit flaky, will cover the details at the end of this post)
Lets take an example which is quoted in the documentation of Ektorp itself, a BlogPost and a bunch of Comments. What if we want to store a BlogPost and Comment as different documents, can we add comments to the blog post and just by saving blog post does it save all the comments as individual documents ? the answer is yes, its is possible with @DocumentReferences annotation.
BlogPost.java

Comment.java

Blog post has a collection of comments, having a @DocumentReferences annotation on the collection comments will enable us to save a blog post by adding comment and the comment is saved as a separate document behind the scenes. However there is a trick to get this working.
1. The cascade property in the @DocumentReferences annotation, it has to be either set to ALL or SAVE_UPDATE to get this working.
2. Comment (which is a child in this case) should have a back reference to the parent with its id (blogPostId in this case) and the same has to be specified in the @DocumentRefernces annotation.
3. The most important thing is while adding a comment to the blogPost, this back reference in the comment has to be set manually. This means that before adding any children to the parent, the parent has to be persisted in the database before only then we will have the id generated for the parent which can be set as a back reference in the children. We expect typical ORM like hibernate to do this automatically for us in case of traditional RDBMS but in with ektorp we have to take care of all this manually.
Find the test below which demonstrates all the three points above.
BlogPost.java
BlogPostIntegrationTest.java


Notice that in the test, BlogPost if first created and persisted, post which a new comment is added to the blog post and just the blog post is updated this operation will create a comment as a separate document (behind the scenes). Like I mentioned before while adding a comment to the blogPost we should also set the blogPostId (backreference) in the comment explicitly.

Ektorp has documented the limitations of this usage but the example itself is not very clear hence this post :)

7 comments:

Ciara Ride Lyrics said...

You really give the right information that I am looking for.
I found this blog post through Google and I love your contents here.
Thanks for sharing such informative articles and I will make sure visit some other blog post on your websites to read more.
facebook youtube

Unknown said...

Interesting post.Continue sharing more.
Regards,
J2EE Training in Chennai

andrea chiu said...

Thank you for sharing your thoughts and for inspiring us.
Keep it up and continue on what your doing. Visit my site too.

triciajoy.com

www.triciajoy.com

Aditya wibowo said...

You really give the right information

Termite Insect

Dian said...

I am very happy with what you wrote
This is really one of the articles that I think is very interesting in terms of its contents
Thank you very much for sharing this pretty good information

Obat Penyakit Gondongan Di Apotik Cara Mengobati Kutu Air Di Sela Jari Kaki & Tangan Obat Penghilang Benjolan Di Payudara Karena Kanker / Non-Kanker Pengobatan Asma yang Direkomendasikan Oleh Dokter Bahaya Sering Cegukan / Sendawa Cara Mengatasi Vagina yang Terasa Gatal Bahaya Benjolan di Ketiak Obat Sakit Pinggang Akibat Kecetit Obat Rangen Cara Menghilangkan Gatal Pada Kulit

Aparna said...

This is the awesome post and I have huge information from your creative blog. The admin presented the content is very interesting and also comprehensive. Thank you for your sharing, Keep updating...
Oracle Training in Chennai
Oracle Training institute in chennai
Tableau Training in Chennai
Oracle DBA Training in Chennai
Primavera Training in Chennai
Unix Training in Chennai
Power BI Training in Chennai
Oracle Training in Chennai
Oracle Training institute in chennai

Reshma said...

Thanks for this wonderful blog. keep update more information about this
Ethical Hacking Course in Chennai
Hacking Course in Chennai
Ethical hacking course in bangalore
Ethical hacking course in coimbatore
Hacking course in bangalore
Ethical hacking in bangalore
Ethical hacking training in bangalore
ielts coaching centre in coimbatore
Software Testing course in Bangalore