Wednesday, June 13, 2012

JsonMappingException when serializing POJO reverse engineered using Hibernate, to JSON

We were working recently on a Java project with Hibernate as the ORM. We created the database and reverse engineered the entities to POJO using Hibernate (See how).

I am putting up a simple ER diagram to demonstrate the issue.

Each Item has a list of item details which are name/value pairs.



And we needed to serialize this object to JSON so we used Jackson. And when trying serialize we were confronted with an exception.

org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: ItemDetail.item, no session or session was closed

Closely looking at the generated ItemDetail class I found that there was a Item defined there. 

   @ManyToOne(cascade={}, fetch=FetchType.LAZY)  
   @JoinColumn(name="item_id", unique=false, nullable=false, insertable=true, updatable=true)  
   public Item getItem() {  
     return this.item;  
   }  
   public void setItem(Item item) {  
     this.item = item;  
   }  

The first (stupid) thing I tried was to change to FetchType from LAZY to EAGER. which I thought would solve the problem.  However that resulted in a StackoverflowException because the serializer kept recursing and it would never reach the end.

 Exception in thread "main" org.codehaus.jackson.map.JsonMappingException: Infinite recursion (StackOverflowError) (through reference chain: ItemDetail["item"])

The problem here is that Hibernate provides us a way to navigate back using the foreign key relationship where in this instance to get the parent Item that belongs to each ItemDetail. But Jackson did not like it!

The solution was VERY simple.

Simply inform the serializer to ignore such parent relationships. You can inform Jackson to leave it aside using @JsonIgnore (See all the annotations here).

   @ManyToOne(cascade={}, fetch=FetchType.LAZY)   
   @JoinColumn(name="item_id", unique=false, nullable=false, insertable=true, updatable=true)   
   @JsonIgnore  
   public Item getItem() {   
    return this.item;   
   }   

And Jackson did not try to serialize it and it worked just as expected!

Saturday, June 2, 2012

Setup and forget automated backup with Cobian + Your favorite cloud hosting app!

There are numerous file cloud syncing/backup tools available such Sugar Sync, Dropbox, SkyDrive, Box.net and Google Drive to name a few.



These tools helps you keep your files safe in the event of a disk failure, virus attack or any other destructive situation, automatically.

You add a file/folder to the "special" sync folder and all is backed up and synced or use "selective sync", a highly sought after feature implemented in most applications, to select which folder to be synced to the cloud.

Now this is all good. But there are times when you really don't want some files to be synced always, all the time. The perfect example is the Outlook PST file which stores all your emails. You want it to be backed up, but it's going to be a waste of bandwidth if each time an email arrives and your file starts syncing to the cloud. There maybe many other such files that need not be synced ASAP.

What you need is some way to specify WHEN exactly do you want the sync to happen. How awesome would it be if you can tell "Hey, backup this file/folder every hour, every 135 minutes, every week or every first Monday and Thursday" ?

Cobian Backup to the rescue! I was looking for a flexible backup solution and found a gem. Luis Cobian has created a wonderful piece of software. Kudos!

Download Cobian Backup version 11 (Code named Gravity) , the latest as of writing and install it. I am assuming you have your sync tool installed as well.

Once Cobian Backup is installed, in the "Task" menu, click "New Task" or alternatively press Ctrl + A to add a task.


Note the setting "Use Volume Shadow Copy". Using that option, you can be sure that your file can be backed even while it's locked by an application.

After that add a source which could be any file or folder that you want to backup. The destination should be your magic folder (Dropbox/SugarSync Magic Briefcase") or any folder setup to be selectively synced. You can have multiple sources and multiple destinations.



As you can scheduling is very advanced and will cater to almost any need.


There's compression + industry standard encryption too!


with filters!



 I unfortunately cannot go through all the features in Cobian backup due to the sheer size!

So the idea is to use Cobian Backup to backup your files to the synced folder and you have a bullet proof, automated backup!

Download Cobian Backup