Wednesday, December 11, 2019

Different ways to retrieve Data in Hibernate || Spring JPA

HQL
Native Query
Criteria

HQL is to perform both select and non-select operations on the data,  but Criteria is only for selecting the data, we cannot perform non-select operations using criteria
HQL is suitable for executing Static Queries, where as Criteria is suitable for executing Dynamic Queries
HQL doesn’t support pagination concept, but we can achieve pagination with Criteria
Criteria used to take more time to execute then HQL
With Criteria we are safe with SQL Injection because of its dynamic query generation but in HQL as your queries are either fixed or parametrized, there is no safe from SQL Injection.

HQL:

Please note that Hibernator’s query facilities do not allow you to alter the database structure. We can alter only data inside tables.
https://www.boraji.com/hibernate-5-native-sql-query-example
https://howtodoinjava.com/hibernate/complete-hibernate-query-language-hql-tutorial/

https://www.javaguides.net/2018/11/hibernate-query-language-insert-update.html

https://www.javaguides.net/2018/11/hibernate-5-hql-crud-example-snippets.html


Spring JPA


@Query("SELECT u FROM User u WHERE u.status = ?1")
User findUserByStatus(Integer status);
 
@Query("SELECT u FROM User u WHERE u.status = ?1 and u.name = ?2")
User findUserByStatusAndName(Integer status, String name);

https://www.baeldung.com/spring-data-jpa-query

Named Query:
https://www.boraji.com/hibernate-5-named-query-example
https://howtodoinjava.com/hibernate/hibernate-named-query-tutorial/

Criteria Query

https://howtodoinjava.com/hibernate/hibernate-criteria-queries-tutorial/

https://www.boraji.com/hibernate-5-criteria-query-example

No comments:

Post a Comment