Spring Data JPA Using Hibernate

 

ORM (Object Relational Mapping):

- Process of mapping a Java class to Database tables

- Developers can deal with Objects instead of writing SQLs. 

 

JPA (Java Persistence API) :

 - Its a standard from Oracle to perform ORM in Java EE applications.

- JPA comes with Specification & API

            Specification -> For JPA Vendors/Providers like Hibernate, Open JPA , Eclipse Link and etc..

            API -> For developers


Why JPA:

Before JPA, we have to learn each ORM tool like Hibernate / Open JPA and etc depends on the ORM  we used in our application. 

Now, we can learn one single API (JPA). All the vendors/Providers implements JPA.

So, we can switch from one vendor to another without making any code change in our application, 


JPA API:

Two important classes:

EntityManagerFactory

EntityManager

Spring Data will hide these APIs, we need not deal with the above classes :)

     Also, a lot of annotations are available. For Eg:

                    To Map java classes to Table  : @Entity , @Table

                    To Map variable to Database table columns: @Id , @column


Spring Data:

- A framework that removes a lot of configuration and coding in the Data Access Layer of our application (Need not add DAO, implementation and etc.).

- Simply add Entity class (Eg: Employee), define an Interface (Eg: EmployeeRepository), and extend the CRUDRepository class of Spring. 

 - From the service, we can use our Repository interface (EmployeeRepository) and invoke the Save/findOne/findAll/delete/count methods that will perform CRUD operations. 

- Spring data provides 

        Finder Methods

        Paging & Sorting

        JPQL

        Native queries


- Inheritance 

- Component Mapping:

- Relationships in hibernate

    @OneToOne

    @OneToMany

    @ManyToOne

    @OneToMany


- Caching


    Level-1: Session

- Data will be shared from the cache for the particular session.

- By default it will be enabled. 

 

    Level-2: SessionFactory 

- Data here is shared across the sessions.

- Hibernate does not have inbuilt support. We can use caching providers such as ehcache, Swaram cache, Jboss Tree cache, OS cache and etc. 


Transaction Management:





Comments

Popular posts from this blog

Java Design Patterns