Issue
I am new to spring framework and hibernate. I learned HibernateTemplate class functions like save(), update(), delete(), load(), loadAll() etc. In all these functions we are doing only reading, updating, deleting, updating functions only. In SpringJdbc we can execute our custom SQL queries(as string) also using functions like query or execute but in hibernate with spring, I am not able to see any method that we can use to run our custom query and get the result as list of persistent objects. Is there any way by which we can do it in spring hibernate?
Solution
Hibernate is just an ORM implementation. ORM = Object-relational-mapping .
Therefore Hibernate only does what it was created to do. Create the link between java objects and database objects
Also Hibernate is just an implementation that is used by default from Spring. You could as well chose another implementation. You normally call spring methods which then get executed by Hibernate.
If you use Spring Jpa you can find many methods to achieve that. One of them are Native Queries in repositories.
Example
@Repository
public interface CarRepository extends CrudRepository<Car, Long>, PagingAndSortingRepository<Car, Long> {
@Query(value="select * from car a where a.model= :model", nativeQuery=true)
List<Car> getCarsByModel(String model);
}
Answered By - Panagiotis Bougioukos
Answer Checked By - David Marino (JavaFixing Volunteer)