What is Spring data JPA ?

  • The too much boilerplate code has to be written to execute simple queries as well as perform pagination, and auditing.
  • It aims to significantly improve the implementation of data access layers by reducing the effort to the amount that’s actually needed.
  • As a developer, you write your repository interfaces, including custom finder methods, and Spring will provide the implementation automatically.
  • We can remove some boilerplate code by creating an abstract base repository class that provides CRUD operations for our entities. This means that we can create concrete repositories by extending that base repository class.
  • It has a lot of customization points, including items like query auto-generation by convention, query hints, and auditing.

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.

Advantages

  • Don’t need to write persisting, updating, deleting or searching logic.
  • Reduce a lot of basic DB code.
  • No need to create implementation classes to perform basic CRUD operations.
  • Just extends Repository class and it takes the domain class to manage as well as the id type of domain class as type arguments in Interface.
  • The interface acts as a marker interface to capture the type of work and to help you to discover interfaces that extend this one.

Types of Repositories

public interface Repository public interface CRUDRepository
public interface PagingAndSortingRepository
public interface JpaRepository

For more detail, Please watch below video –

Thanks for reading πŸ™‚

Please Subscribe our you tube channel Almighty Java

 

What is Entity Manager in Hibernate ?

  • Hibernate Entity Manager implements the programming interfaces and lifecycle rules as defined by the JPA 2.0 specification.
  • Together with Hibernate Annotations, this wrapper implements a complete (and standalone) JPA persistence solution on top of the mature Hibernate Core.
  • The Entity Manager API is used to access a database in a particular unit of work. This interface is similar to the Session in Hibernate.
  • It is used to create and remove persistent entity instances, to find entities by their primary key identity, and to query over all entities.
<!– Need to use below dependency in pom.xml file to integrate entity manager -->
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
    <version>5.2.5.Final</version>
</dependency>

What is Entity Manager Factory?

  • An entity manager factory provides entity manager instances, all instances are configured to connect to the same database, to use the same default settings as defined by the particular implementation, etc.
  • Interface used to interact with the entity manager factory for the persistence unit. When the application has finished using the entity manager factory, and/or at application shutdown, the application should close the entity manager factory.
  • You can prepare several entity manager factories to access several data stores. This interface is similar to the Session Factory in native Hibernate.

What is Local Container Entity Manager Factory Bean?Β 

  • FactoryBean that creates a JPA EntityManagerFactory according to JPA’s standard container bootstrap contract.
  • This is the most powerful way to set up a shared JPA EntityManagerFactory in a Spring application context.
  • The EntityManagerFactory can then be passed to JPA-based DAOs via dependency injection.
  • Note that switching to a JNDI lookup or to a LocalEntityManagerFactoryBean definition is just a matter of configuration.

What is Persistence context?

  • The scope of this context can either be the transaction, or an extended unit of work.
  • Entities are managed by javax.persistence.EntityManager instance using persistence context.
  • Each EntityManager instance is associated with a persistence context.
  • Within the persistence context, the entity instances and their lifecycle are managed.
  • Persistence context defines a scope under which particular entity instances are created, persisted, and removed.
  • A persistence context is like a cache which contains a set of persistent entities, So once the transaction is finished, all persistent objects are detached from the EntityManager’s persistence context and are no longer managed.

What is HibernateJpaVendorAdapter?

  • JpaVendorAdapter implementation for Hibernate EntityManager.
  • Its kind of one property of EntityManager and contains some configuration like generate DDL, show SQL, format SQL etc.
<!-- Entity Manager Factory -->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" p:dataSource-ref="dataSource" p:packagesToScan="com.mightyjava.model">
   <property name="jpaVendorAdapter">
       <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" p:showSql="true" p:generateDdl="true"/>
   </property>
</bean>  

What is JpaTransactionManager?

  • This transaction manager is appropriate for applications that use a single JPA EntityManagerFactory for transactional data access.
  • JTA (usually through JtaTransactionManager) is necessary for accessing multiple transactional resources within the same transaction.
  • This transaction manager also supports direct DataSource access within a transaction.
<!-- Transaction -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager" p:entityManagerFactory-ref="entityManagerFactory" />

For more detail, Please watch below video –

Thanks for reading πŸ™‚

Please Subscribe our you tube channel Almighty Java