Sobes.tech
Junior — Middle

Can you explain what a native SQL query means in the context of Hibernate and how it is used?

sobes.tech AI

Answer from AI

A native SQL query in Hibernate is a direct SQL query to the database that allows executing operations without using HQL (Hibernate Query Language) or Criteria API.

It is used when:

  • You need to perform a complex or specific query that is difficult or impossible to express using Hibernate.
  • Performance optimization is required.

Example of using a native SQL query:

String sql = "SELECT * FROM users WHERE status = :status";
List<User> users = session.createNativeQuery(sql, User.class)
    .setParameter("status", "ACTIVE")
    .getResultList();

Here, createNativeQuery allows executing SQL directly, and the result is mapped to Hibernate entities.

Can you explain what a native SQL query means in the… - sobes.tech