How to call native SQL queries with JPA and Hibernate

How to call native SQL queries with JPA and Hibernate

JPQL is the most common way to query data from a database with JPA. It enables you to reuse your mapping definitions and is easier to use than SQL. But it supports only a small subset of the SQL standard, and it also provides no support for database-specific features.

So what shall you do if you need to use a database-specific query feature or your DBA gives you a highly optimized query that you can’t transform into JPQL? Just ignore it and do all the work in the Java code?

Of course not! JPA has its own query language, but it’s designed as a leaky abstraction and supports native SQL queries. You can create these queries in a similar way as JPQL queries, and they can even return managed entities if you want.

In this video, I will show you how to use native SQL queries, different options to map the query result to DTOs and entity objects and avoid a common performance pitfall.

Links mentioned in the video:
https://thorben-janssen.com/jpql/
https://thorben-janssen.com/dto-projections/
https://thorben-janssen.com/persist-postgresqls-jsonb-data-type-hibernate/
https://thorben-janssen.com/jpa-native-queries/#resultHandling
https://thorben-janssen.com/jpa-2-2-repeatable-annotations/
https://thorben-janssen.com/result-set-mapping-basics/
https://thorben-janssen.com/result-set-mapping-complex-mappings/
https://thorben-janssen.com/result-set-mapping-constructor-result-mappings/
https://thorben-janssen.com/result-set-mapping-hibernate-specific-mappings/
https://thorben-janssen.com/entities-dtos-use-projection/
https://thorben-janssen.com/hibernate-resulttransformer/
https://thorben-janssen.com/hibernate-query-spaces
https://thorben-janssen.com/tips-to-boost-your-hibernate-performance/


Like my channel? Subscribe!
http://bit.ly/2cUsid8

Read the accompanying post: https://thorben-janssen.com/jpa-native-queries/

Want to connect with me?
Blog: https://thorben-janssen.com/
Twitter: https://twitter.com/thjanssen123
Facebook: https://www.facebook.com/thorbenjanssenofficial
Linkedin: https://www.linkedin.com/in/thorbenjanssen/

jpahibernateSQL

Post a Comment

0 Comments