Annotation Interface SQLSelect


@Target({TYPE,FIELD,METHOD}) @Retention(RUNTIME) public @interface SQLSelect
Specifies a custom SQL query to be used in place of the default SQL generated by Hibernate when an entity or collection is loaded from the database by id. This occurs when:

The given SQL statement must have exactly the number of JDBC ? parameters that Hibernate expects, that is, one for each column of:

  1. the primary key, in the case of an entity, or
  2. the foreign key, in the case of a collection.

If the tables which affect the query results are specified, then changes to those tables will be flushed before execution of the query.

For example:

 @SQLSelect(sql = """
                  SELECT id, created, text
                  FROM records
                  WHERE id = ? and deleted is null
                  """
            querySpaces = "records")
 @Entity
 public class Record { ... }
 

Optionally, an explicit result set mapping may be specified. It should have:

  1. a single EntityResult, if the SQL query loads an entity, one-to-many association, or many-to-many association, or
  2. a single ColumnResult or ConstructorResult, if the SQL query loads an collection of basic-typed values.
Since:
6.2
See Also: