Saturday, December 21, 2013

Hibernate the Persistence Provider

Hibernate the Persistence Provider

Hibernate is a most interesting framework I'm using and used.

This is an implementation of JPA(Java persistence API) standards. There are several other implementations like EclipseJPA.
This to recap the important points of the Hibernate/JPA. This writing would be more into conceptual rather than technical.

Set-up
Setting up the Hibernate to over project is the first step everybody do/ need to do.
Choosing database driver, setting up the data source, connection pool and transaction manager with username password, driver class, dialect, etc. Finally introducing the persistence unit for the application.
This depends on the technology stack, if the application plan is to deploy it in a application server the set-up would be creating the data source in whatever application server and pointing the JNDI name in the persistence.xml.

ORM starts...
Starting to write the entity classes and relationship between them. Entity classes are nothing but a set of POJO's, and in additionally which contains mapping metadata.
Here where the mapping starts. The ORM step is building a mapping/contract between the Java code and the database. Telling that I'm representing this POJO for this particular database table and relationship between table.

When doing the mappings developers should notice a point her, each implementations of JPA provides its own annotations. What if you decided to change your persistence provider from Hibernate to OpenJPA?
Trick is never use implementation API's, always use standard JPA API's which is available under package javax.persistence.* so the you can swap your provider any time.

Under ORM mapping interesting part is relationship mapping...
@OneToOne
@OneToMany
@ManyToOne
@ManyToMany

Each can be bidirectional or unidirectional, bidirectional means both party knows each other in other word both side reference mapping would be there. Unnecessary bidirectional is not recommended.
Mapping needs to be discussed in detail, may be in another separate time.

Query Writing
JPQL Java Persistence Query language is the alternative for SQL which is used for query the database when JPA is used.

In my past experiences I consider following is an important points that an Hibernate expert would know rather than basics

+ What is the output type of the JPA/Hibernate native query?
Answer : Object / Array of object types

+ What is the mapping should be used if the relation should be deleted while updating with detached?
Answer : orphanRemoval=true

No comments:

Post a Comment