Saturday, September 2, 2017

Spring Hibernate JPA Integration

In hibernate framework, we provide all the database information hibernate.cfg.xml file.

But if we are going to integrate the hibernate application with spring, we don't need to create the hibernate.cfg.xml file. We can provide all the information in the applicationContext.xml file.

Advantage of Spring framework with hibernate

The Spring framework provides HibernateTemplate class, so you don't need to follow so many steps like create Configuration, BuildSessionFactory, Session, beginning and committing transaction etc.

So it saves a lot of code.

In this file, we are providing all the informations of the database in the BasicDataSource object. This object is used in the LocalSessionFactoryBean class object, containing some other informations such as mappingResources and hibernateProperties. The object of LocalSessionFactoryBean class is used in the HibernateTemplate class.
hibernate template
 Let's see the code of applicationContext.xml file.

    <?xml version="1.0" encoding="UTF-8"?> 
    <beans 
        xmlns="http://www.springframework.org/schema/beans" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xmlns:p="http://www.springframework.org/schema/p" 
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> 
     
        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> 
            <property name="driverClassName"  value="oracle.jdbc.driver.OracleDriver"></property> 
            <property name="url" value="jdbc:oracle:thin:@localhost:1521:xe"></property> 
            <property name="username" value="system"></property> 
            <property name="password" value="oracle"></property> 
        </bean> 
         
        <bean id="mySessionFactory"  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 
            <property name="dataSource" ref="dataSource"></property> 
             
            <property name="mappingResources"> 
            <list> 
            <value>employee.hbm.xml</value> 
            </list> 
            </property> 
             
            <property name="hibernateProperties"> 
                <props> 
                    <prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop> 
                    <prop key="hibernate.hbm2ddl.auto">update</prop> 
                    <prop key="hibernate.show_sql">true</prop> 
                     
                </props> 
            </property> 
        </bean> 
         
        <bean id="template" class="org.springframework.orm.hibernate3.HibernateTemplate"> 
        <property name="sessionFactory" ref="mySessionFactory"></property> 
        </bean> 
         
        <bean id="employeeDao" class="com.javatpoint.EmployeeDao"> 
        <property name="template" ref="template"></property> 
        </bean> 
        
        </beans>   


Spring with JPA, using Hibernate as a persistence provider.

To use JPA in a Spring project, the EntityManager needs to be set up. 
This is the main part of the configuration – and it is done via a Spring factory bean – either the simpler LocalEntityManagerFactoryBean or the more flexible LocalContainerEntityManagerFactoryBean.

<!-- Simple implementation of the standard JDBC DataSource interface,
        configuring the plain old JDBC DriverManager via bean properties -->

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
      <property name="driverClassName" value="com.mysql.cj.jdbc.Driver" />
      <property name="url" value="jdbc:mysql://localhost:3306/spring_jpa" />
      <property name="username" value="tutorialuser" />
      <property name="password" value="tutorialmy5ql" />
   </bean>


<!-- This produces a container-managed EntityManagerFactory;
         rather than application-managed EntityManagerFactory -->
<bean id="myEntityFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
      <property name="dataSource" ref="dataSource" />
      <property name="packagesToScan" value="org.baeldung.persistence.model" />
      <property name="jpaVendorAdapter">
         <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
      </property>
      <property name="jpaProperties">
         <props>
            <prop key="hibernate.hbm2ddl.auto">create-drop</prop>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
         </props>
      </property>
   </bean>


<!-- 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. -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
      <property name="entityManagerFactory" ref="myEntityFactory" />
   </bean>
   


<!-- responsible for registering the necessary Spring components that power annotation-driven transaction management;
        such as when @Transactional methods are invoked -->

<tx:annotation-driven />  


entityManagerFactoryBean : LocalEntityManagerFactoryBean produces an application-managed EntityManagerFactory whereas LocalContainerEntityManagerFactoryBean produces a container-managed EntityManagerFactory. It supports links to an existing JDBC DataSource, supports both local and global transactions.

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. Note that you need to configure your JPA provider accordingly in order to make it participate in JTA transactions. Of course, JtaTransactionManager does require a full JTA-supporting application server, rather than a vanilla servlet engine like Tomcat.


tx:annotation-driven : enable the configuration of transactional behavior based on annotations e.g. @Transactional. The @EnableTransactionManagement annotation provides equivalent support if you are using Java based configuration. To do this. simply add the annotation to a @Configuration class.

Next important classes are DAO classes which use the entity manager to perform CRUD operations using hibernate entities, and specifying methods which support transactions through @Transactional annotation. In our case, we have applied @Transactional annotation at class level, making all public methods transactional.


@Repository
@Transactional
public class EmployeeDAOImpl implements EmployeeDAO
{
    @PersistenceContext
    private EntityManager manager;



public List<EmployeeEntity> getAllEmployees()
    {
        List<EmployeeEntity> employees = manager.createQuery("Select a From EmployeeEntity a", EmployeeEntity.class).getResultList();
        return employees;
    }
..

}


@PersistenceContext expresses a dependency on a container-managed EntityManager and its associated persistence context. @Repository is usually applied on DAO layer. 


JPA in Spring Boot

The Spring Boot project is intended to make creating Spring applications much faster and easier. This is done with the use of starters and auto-configuration for various Spring functionalities, JPA among them.

To enable JPA in a Spring Boot application, we need the spring-boot-starter and spring-boot-starter-data-jpa dependencies:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
    <version>1.5.3.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
    <version>1.5.3.RELEASE</version>
</dependency>

The spring-boot-starter contains the necessary auto-configuration for Spring JPA, and the spring-boot-starter-jpa project references all the necessary dependencies such as hibernate-entitymanager.

Spring Boot configures Hibernate as the default JPA provider, so it’s no longer necessary to define the entityManagerFactory bean unless we want to customize it.

Spring Boot can also auto-configure the dataSource bean, depending on the database used. In the case of in-memory database of type H2, HSQLDB and Apache Derby, Boot automatically configures the DataSource if the corresponding database dependency is present on the classpath.

For example, if we want to use an in-memory H2 database in a Spring Boot JPA application, we only need to add the h2 dependency to the pom.xml file:

<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <version>1.4.195</version>
</dependency>


This way, we don’t need to define the dataSource bean, but we can do so if we want to customize it.

If we want to use JPA with MySQL database, then we need the mysql-connector-java dependency, as well as to define the DataSource configuration.

This can be done in a @Configuration class, OR by using standard Spring Boot properties.
The Java configuration looks the same as it does in a standard Spring project:

@Bean
public DataSource dataSource() {
    DriverManagerDataSource dataSource = new DriverManagerDataSource();

    dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
    dataSource.setUrl("jdbc:mysql://localhost:3306/myDb?createDatabaseIfNotExist=true");
    dataSource.setUsername("mysqluser");
    dataSource.setPassword("mysqlpass");

    return dataSource;
}

                                                                         OR


To configure the data source using a properties file, we have to set properties prefixed with spring.datasource:


spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/myDb?createDatabaseIfNotExist=true
spring.datasource.username=mysqluser
spring.datasource.password=mysqlpass

Spring Boot will automatically configure a data source based on these properties.

1 comment:

Web Development

Design Phase:- Below all these represent different stages of the UX/UI design flow:- Wireframes represent a very basic & visual repr...