1.

How do you go about integrating Spring Boot with Hibernate?

Answer»

Hibernates gels well with Spring and with Spring Boot around, it makes the task super easy.  We will see a generic way of Integrating Hibernate using Spring Boot in such a way that switching to some other jpa vendor tomorrow would be super easy.

Let’s start with the process.

1. First thing first, you will need spring-boot-starter-data-jpa.

Since Spring uses Hibernate as default vendor, you really don't need to configure or mention Hibernate explicitly somewhere.

2. Second step would be Drafting the ENTITY Class. Let’s consider, we are building an application to get a list of employees. So Employee, in this case, would be an Entity for us. You can use @javax.persistence.Entity annotation to DEFINE your entity as :

@Entity

public class Employee {

@Id

    @GeneratedValue

private Long id;

private String name;

//……./// Getters & Setters

3. Extend the JpaRepository and define methods ( if required).

Your get all CRUD operations method inbuilt and if you need any additional customized methods, you can define them here.

In our example, we don't need to define any method as a JPA repository does provide findAll() method.

@Repository

public interface EmployeeRepository extends JpaRepository<Employee, Long>{

}

Actually, that’s it, you are DONE from an Integration point of view. Of course, you will need a few additional things from an OPERATIONAL point of view that we would define below. 

4. Table Naming in Capitals :

For this set following property in application.properties

spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

5. For Operational purpose in this example, we are using H2, so for that : 

  a) INCLUDE H2 maven dependency in pom.xml 

<dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> </dependency>

  b) Enable H2 by setting following property in application.properties 

spring.h2.console.enabled=true

6. Define EmployeeService :

@Service public class EmployeeService { @Autowired private EmployeeRepository employeeRepository; public List<Employee> getAllEmployees() { return employeeRepository.findAll(); } }

 7. Include Spring-boot-web-starter in pom.xml abd Define EmployeeController :

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> @RestController @RequestMapping("/employees") public class EmployeeController { @Autowired private EmployeeService employeeService; @GetMapping public List<Employee> getAllEmployees() { return employeeService.getAllEmployees(); } }

The Code is available in GitHub @

Now Start the application and hit http://localhost:8080/h2-console

Change JDBC url to “jdbc:h2:mem:testdb”.

Leave others with default values and login and execute the following queries :

insert into EMPLOYEE values(1, ‘Virat’);

insert into EMPLOYEE values(2, ‘Rohit’);

insert into EMPLOYEE values(3, ‘Rahul’);

Now hit the http://localhost:8080/employees endpoint and see the list of employees just entered.



Discussion

No Comment Found