Interface StatementReference

All Superinterfaces:
Reference
All Known Implementing Classes:
StaticStatementReference

public non-sealed interface StatementReference extends Reference
A reference to an executable named statement for an operation that returns a row count. The named statement is declared via the NamedQuery or NamedNativeQuery annotations, or using JakartaQuery or NativeQuery. An instance of StatementReference is usually obtained from the static metamodel of the annotated type.

In this example, a method is annotated, and the name of the statement is determined by the name of the annotated method:

class Filer {
    @Inject EntityManager entityManager;

    @JakartaQuery("delete from Record where temporary = true")
    int purgeTemporaryRecords() {
        return entityManager.createStatement(Filer_.purgeTemporaryRecords())
                .execute();
    }
}

In this example, the entity class is annotated, and the NamedQuery annotation explicitly specifies a name:

@NamedQuery(name = "updateSales",
            query = "update Book set sales = ?1 where isbn = ?2")
@Entity class Book { .. }

In this case the StatementReference obtained from its static metamodel does not include arguments to the query parameters, and so they must be supplied via setParameter:

int updated =
        entityManager.createStatement(Book_._updateSales_)
                .setParameter(1, sales)
                .setParameter(2, isbn)
                .execute();

A StatementReference may include arguments to parameters of the query.

In the Jakarta Persistence query language, a DELETE or UPDATE statement is not a typed query and is always represented by an instance of StatementReference. A native SQL query is represented by an instance of StatementReference if it returns a row count. A Jakarta Persistence SELECT query, or a native SQL query that returns a result set, is usually represented as an instance of TypedQueryReference.

Since:
4.0
See Also: