Native queries
On This Lesson 4 topics
This lesson is planned. The outline below lists the topics it will cover. The full lesson has not been published yet.
In this lesson
use plain SQL when JPQL cannot express the query.
Planned topics
@Query(nativeQuery = true)runs real SQL against the database.- Use it for database-specific features (window functions, full-text search) and heavy reports.
- It is not portable and bypasses entity names; refactoring does not update it.
- Default to JPQL or derived queries; native is the exception.
- Pair native queries with projections, not entities, for reports.
Draft outline details
Demonstration
@Query(value = "select c.name as category, count(*) as total from product p " +
"join category c on c.id = p.category_id group by c.name", nativeQuery = true)
List<CategoryCount> countPerCategory();Check your understanding
Should native queries be your default?
Show answer
No; use them only when needed.
Practice
Apply this lesson with the module's lab, exercises and quiz.
Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.