Spring Boot run native SQL Query

Here is an example code of how to run native SQL query with parameter in Spring Boot. First we need use Spring boot’s EntityManagerFactory bean. We can simply use @Autowired annotation to make this available in our class. Then create new EntityManager session, run the native SQL and finally close the session. package com.firstfewlines.service; import org.springframework.beans.factory.annotation.Autowired; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.NoResultException; import java.util.Date; public class NativeSqlExample { @Autowired private EntityManagerFactory entityManagerFactory; public Double getDayPrice(String scrip, Date prdate){ EntityManager session = entityManagerFactory.

Spring Boot JPA Custom Query method

The CrudRepository interface provides the CRUD functionality in Spring JPA. It comes with default definitions of methods to execute common operations like find, save and delete records. To work with a domain class, we need to inherit the CrudRepository to create a new interface corresponding to the domain class. Here we will see how to write custom query method with parameter and sorting by the example of Student domain class with it’s StudentRepository.