First of all let us understand what is Object Relational mapping(ORM). ORM is a technique to map java objects to tabular data. Here tabular data means our relational database. When it comes to an object, it has inheritance property, object entity(==), object equality(equals) and many others which creates a mismatch while dealing the same object with a tabular data format. In tabular format, we have a table. A table has many rows and columns. We can have an object that contains another object and like this a huge chain of objects. But in from relational database perspective we don't have one table containing another. But we have joins and union ro deal with multiple tables using SQL.
Whatever the mismatch between object representation of data and tabular data, it is required to process those tabular data and implement logic on top of that in object oriented programming language and deliver the meaningful result to clients. ORM comes into picture when we try to deal with the tabular format of data from object persective. ORM is a tool that provides mapping between the tabular data format and object representation of data.
Different ORM Implementations
Now question comes what are the different implementations of ORM meaning what are the tools for object relational mapping. Before that we require a specification, a standard rules defined that these tools follow to be an ORM implementation. While dealing with specification, JPA comes into picture. JPA stands for Java Persistence API. It provides specification for the ORM implementations. There many imlemmentaations of ORM such as Hibernate(most widely used), Open JPA, eclipse link etc. Google can answer many others.
While dealing with hibernate, you need to define some configurations in a xml file and use some annotations and start writing your DAO layer with object perspective. If you are using Spring, then Spring provides nice integration with hibernate. Even if you integrate hibernate with Spring there are many boiler plate codes required to execute a simple select query. Though there are many helpers method provided by hibernate to perform different operations on DB, but still boiler plate codes are required in all the implementations.
To get rid of this boiler plate code, Spring came up with Spring data.Let us discuss about spring data features.
Features:
Sophisticated support to build repositories based on Spring and JPA
Support for Querydsl predicates and thus type-safe JPA queries
Transparent auditing of domain class
Pagination support, dynamic query execution, ability to integrate custom data access code
Validation of @Query annotated queries at bootstrap time
Support for XML based entity mapping
JavaConfig based repository configuration by introducing @EnableJpaRepositories.
Spring data also provides integration with hibernate. If you dont want to go with JPA , you can also have your hibernate implementations. To get started with spring Data JPA, you can take an example here - http://www.devglan.com/spring-boot/spring-data-jpa-example
Though you are free to use any database like mysql, oracle or sql server, spring also provides convenient ways to deal with in memory databases such as H2 during development phases. You can find an example here - spring boot h2 database example
This is a very short introduction of ORM in java world.
No comments:
Post a Comment