Top Description Methods
org.hibernate.annotations.processing

public @Interface HQL

extends Annotation
Annotations
@Target:METHOD
@Retention:CLASS
@Incubating
Imports
org.hibernate.Incubating, java.lang.annotation.Retention, .Target

Identifies a method of an abstract class or interface as defining the signature of a method which is used to execute the given HQL query, with an implementation generated automatically by the Hibernate Metamodel Generator.

For example:

public interface Books {
    @HQL("from Book where isbn = :isbn")
    Book findBookByIsbn(String isbn);

    @HQL("from Book where title like ?1 order by title offset ?3 fetch first ?2 rows only")
    List<Book> findBooksByTitleWithPagination(String title, int max, int start);

    @HQL("from Book where title like ?1")
    TypedQuery<Book> createBooksByTitleQuery(String title);
}

The Metamodel Generator automatically creates an "implementation" of these methods in the static metamodel class Books_. The generated methods may be called according to the following protocol:

Book book = Books_.findBookByIsbn(session, isbn);
List<Book> books = Books_.findBooksByTitleWithPagination(session, pattern, 10, 0);

Notice the extra parameter of type EntityManager at the start of the parameter list.

Alternatively, the type to which the annotated method belongs may also declare an abstract method with no parameters which returns one of the types jakarta.persistence.EntityManager, org.hibernate.Session, org.hibernate.StatelessSession, or Mutiny.Session, for example:

EntityManager entityManager();
In this case:

Thus, the generated methods may be called according to the following protocol:

Books books = new Books_(session);
Book book = books.findBookByIsbn(isbn);
List<Book> books = books.findBooksByTitleWithPagination(pattern, 10, 0);

This is reminiscent of traditional DAO-style repositories.

For a select query, the return type of an annotated method must be:

For an insert, update, or delete query, the return type of the annotated method must be int or void.

The method parameters must match the parameters of the HQL query, either by name or by position:

As an exception, the method may have:

Queries specified using this annotation are always validated by the Metamodel Generator, and so it isn't necessary to specify the CheckHQL annotation.

Author
Gavin King
Since
6.3
See Also
SQL, Find

Method Summary

Modifier and TypeMethod and Description
public String
Inherited from java.lang.annotation.Annotation:
annotationTypeequalshashCodetoString

Method Detail

valueback to summary
public String value()