Sunday, February 8, 2015

Replacing JPA java serialization engine to JSON

JPA default behavior for storing complex types (classes, containers, etc..) which are used as fields and were not annotated with the @Embeded, @OneToOne, @OneToMany, @ElementCollection... annotation is storing them as byte array in the database.
The byte array is written and read with Java serialization.
Although Java serialization is great for lots of scenarios it has a number of down sides.
In small objects (which is the case lots of times with these blob fields) it generates relative large byte arrays because class meta info it adds per entry.
Another downside is the fact you can't read the values when examining the database.
If you have the following classes:

class Address {
  String country;
  String street;
  String zipCode;
}

class Person {
  public int id;
  public Address;
}

you will find in the database a field named address of type byte array which you can't view from the database level.

This is why replacing the default JPA serialization to other engines can be very helpful.
For example by replacing with few lines of code the engine to Jackson (JSON serialization engine) we managed to reduce blob fields size in 40% and serialization is quicker too.
But the best part is that afterwards instead of finding binary data in the database you will find JSON strings which you can read and even manipulate if needed from database layer (perhaps on application new version upgrade process, stored procedures, selects).
In additional Postgres and other database enables you to create indexes on inner JSON fields which provide you even more added value.

If you are using EclipseLink 2.6 as your JPA provider and postgres as your database, you can benefit from these features / new possibilities by combining the following two features:
java-persistence-performance (Check out the EclipseLink JPA section at the bottom)
datatype-json (Check indexing options)

So you can now store full objects / objects fields as JSON in the database and still index / query them.

* It can be done prior to EclipseLink 2.6 version too.
* If you need code examples let me know
* See also: stackoverflow.com/questions/changing-jpa-eclipselink-java-serialization-to-using-jackson-json

2 comments:

  1. Hello, i came across this post http://stackoverflow.com/questions/28319172/changing-jpa-eclipselink-java-serialization-to-using-jackson-json. where in were trying to solve relationgshipped tables using JPA and transform it into JSON. however we dont know where to implement the listener or what method to override. code example would realy help.

    kind regards

    ReplyDelete
    Replies
    1. Did you check the links above, for specific fields you can use annotations.

      Delete