This article will not be in-depth analysis json type in PostgreSQL, or regular futile attempts to compare this opportunity to PostgreSQL with NoSQL databases such as MongoDB. I just tell you about how to use Hibernate and PostgreSQL json. I think someone it might be useful.

Object within the spirit

Suppose you have a relational data model. It may well be a situation that for some of the entities must store any object (document, if you want). Of course you can expand on that object data model one (or maybe several) entity, or simply store the object as a byte array. But PostgreSQL have long appeared datatype json json object capable of storing in accordance with RFC 4627 . was wondering how to use it and what opportunities it can provide. When the first call to google I found several posts and unstructured Q & A that explains something, but did not give the full picture. After some inquiries and little understood what was happening, I came to the conclusion that the use of these fields is very convenient, and decided to create a little library that simplifies the use of json types. Here I will explain how to use it, there is still a bit compared with the first coming to mind alternatives.

Dialect

Of course neither of which support this type in the standard dialect and not talking. I’d love to have that hibernate itself to recognize the type and carried out validation / schema update. Therefore, the library contains a dialect JsonPostgreSQLDialect extends PostgreSQL9Dialect. Just use this dialect in your persistance.xml.

Object Storage

Store in a json field can be both subject and simply Map. If you want to store the object, it is sufficient to inherit the class that you are going to store PGJsonObject
Public class CacheObject extends PGJsonObject {

}

and make a determination of the type annotations in your Entity:
Entity
TypeDefs ({ TypeDef (name = “JSONObject” , typeClass = CacheObject.class)})
Public class Product {

type (type = “JSONObject” )
Public CacheObject GetCache () {
Return cache;
}
}

If it breaks your schema inheritance, you can create a separate class from your storage class is responsible for the type. He will inherit all the same PGJsonObject, and must override the returnedClass (), which returns the class of the object that you want to store. This class then will need to use in your Entity to determine the type.
Public class JsonCustomType extends PGJsonObject {

Override
Public Class returnedClass () {
Return Custom.class;
}

}

Then type definition in the Entity will look like this:
Entity
TypeDefs ({ TypeDef (name = “JSONObject” , typeClass = JsonCustomType.class)})
Public class Product {

type (type = “JSONObject” )
Public Custom getCustom () {
Return Custom;
}
}

If you are going to store the Map, just use an existing library type JsonMapType.

Recommendations

Be careful with the change data object. If the object that you store not immutable any changes that you make in it will be saved to the database (of course if the transaction does not roll back), so if you do not want to save the changes, make a copy of
Check your native queries. Make sure that they indicate your Entity


Benefits JSON field

Immediately comes to mind two alternative implementations
Extend the schema of a database object
store an array of bytes

The first option can greatly complicate your data model, if the object has a deep hierarchy. If the object is a large array of stores, such as an array of pixels, the table with the points can have a huge number of entries that can slow down the sample. Just when this version of the application must know all about the database schema that would get them out of the table.

In the second embodiment does not require schema extensions, but the information stored in this field is absolutely not representative and not indexed. And as in the previous case, the application needs to know about the structure of the stored data, only this time for proper deserialization.

When using json field all the information in the database is presented in an easy to read format. Just blyagodarya json functions in PostgreSQL can write convenient native query to retrieve specific fields json-object. And thanks to the expression indexes you can hang index on any field of your json-object.
Just use json fields can serve as a great help in the task of integrating. Stored in one place in the field json object in the other, you can pull out of a Map, and vice versa. If you know only part of the structure of the object, you can deserialize only known and desired portion of the object in json-java-object, and the rest were either ignored or deserialize again in the Map. The source code of the project can be found on github . So far, the project is crude. Hopefully in the future ease of use, type annotations.

(Visited 3,344 times, 1 visits today)
adminTips
This article will not be in-depth analysis json type in PostgreSQL, or regular futile attempts to compare this opportunity to PostgreSQL with NoSQL databases such as MongoDB. I just tell you about how to use Hibernate and PostgreSQL json. I think someone it might be useful. Object within the spirit Suppose...

Do you want to be notified about new DBA updates, releases, jobs and free tips? Join our email newsletter. It's fast and easy. You will be among the first to know about hot new DBA updates and stuff, it will help you enhance your DBA skills.
We take your privacy very seriously