Junior — Middle
Kas tähendab kasutada native SQL-päringuid @Query annotatsioonis Hibernate'is?
sobes.tech AI
Vastus AI-lt
In Hibernate, the @Query annotation allows writing queries to retrieve data. By default, it is HQL (Hibernate Query Language) — an object-oriented query language. However, if you want to use a native SQL query, you can specify the parameter nativeQuery = true in @Query.
This means that the query will be executed directly as SQL, without transformation by Hibernate. This is useful when you need to use database-specific functions or complex queries that are difficult to express in HQL.
Example:
@Query(value = "SELECT * FROM users WHERE status = ?1", nativeQuery = true)
List<User> findUsersByStatusNative(String status);
Here, users is the name of the table in the database, not the entity name. This query will be executed directly in the database.