Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion api/src/main/java/jakarta/persistence/QueryFlushMode.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
* @see NamedQuery#flush
* @see NamedNativeQuery#flush
* @see jakarta.persistence.query.QueryOptions#flush
* @see jakarta.persistence.query.StatementOptions#flush
* @see FlushModeType
*/
public enum QueryFlushMode
Expand Down Expand Up @@ -115,4 +116,4 @@ public enum QueryFlushMode
* persistence context.
*/
DEFAULT
}
}
5 changes: 4 additions & 1 deletion api/src/main/java/jakarta/persistence/QueryHint.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import jakarta.persistence.query.QueryOptions;
import jakarta.persistence.query.StatementOptions;

/**
* Supplies a property or hint via an annotation.
Expand All @@ -31,7 +32,8 @@
* {@link NamedNativeStatement#hints @NamedNativeStatement},
* {@link NamedStatement#hints @NamedStatement},
* {@link NamedStoredProcedureQuery#hints @NamedStoredProcedure},
* or {@link QueryOptions#hints @QueryOptions},
* {@link QueryOptions#hints @QueryOptions}, or
* {@link StatementOptions#hints @StatementOptions},
* this annotation specifies a query hint.
* <li>As a nested annotation of {@link Fetch#hints @Fetch},
* it specifies a fetching hint.
Expand All @@ -46,6 +48,7 @@
* @see NamedStatement#hints
* @see NamedStoredProcedureQuery#hints
* @see QueryOptions#hints
* @see StatementOptions#hints
* @see Fetch#hints
*
* @since 1.0
Expand Down
22 changes: 15 additions & 7 deletions api/src/main/java/jakarta/persistence/Reference.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@

/**
* A reference to a named statement or query declared via the
* {@link NamedQuery} or {@link NamedNativeQuery} annotations,
* or using {@link jakarta.persistence.query.JakartaQuery} or
* {@link jakarta.persistence.query.NativeQuery}. An
* {@link NamedQuery}, {@link NamedNativeQuery}, {@link NamedStatement},
* or {@link NamedNativeStatement} annotations, or using
* {@link jakarta.persistence.query.JakartaQuery},
* {@link jakarta.persistence.query.NativeQuery},
* {@link jakarta.persistence.query.JakartaStatement}, or
* {@link jakarta.persistence.query.NativeStatement}. An
* instance of {@code Reference} is usually obtained from the
* static metamodel. This interface declares operations common
* to {@link StatementReference} and {@link TypedQueryReference}.
Expand All @@ -37,10 +40,13 @@ public sealed interface Reference

/**
* The name of the statement or query, as specified by
* {@link NamedQuery#name} or {@link NamedNativeQuery#name},
* {@link NamedQuery#name}, {@link NamedNativeQuery#name},
* {@link NamedStatement#name}, or {@link NamedNativeStatement#name},
* or as inferred from the name of the method annotated
* {@link jakarta.persistence.query.JakartaQuery} or
* {@link jakarta.persistence.query.NativeQuery}.
* {@link jakarta.persistence.query.JakartaQuery},
* {@link jakarta.persistence.query.NativeQuery},
* {@link jakarta.persistence.query.JakartaStatement}, or
* {@link jakarta.persistence.query.NativeStatement}.
*/
@Nullable
String getName();
Expand Down Expand Up @@ -107,7 +113,9 @@ public sealed interface Reference
/**
* A map keyed by hint name of all hints specified via
* {@link NamedQuery#hints}, {@link NamedNativeQuery#hints},
* or {@link jakarta.persistence.query.QueryOptions#hints}.
* {@link NamedStatement#hints}, {@link NamedNativeStatement#hints},
* {@link jakarta.persistence.query.QueryOptions#hints}, or
* {@link jakarta.persistence.query.StatementOptions#hints}.
* <p>
* Any attempted mutation of the returned map results in an
* {@link UnsupportedOperationException}.
Expand Down
32 changes: 16 additions & 16 deletions api/src/main/java/jakarta/persistence/StatementReference.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
/**
* A reference to an executable named statement for an operation
* that returns a row count. The named statement is declared via the
* {@link NamedQuery} or {@link NamedNativeQuery} annotations,
* or using {@link jakarta.persistence.query.JakartaQuery} or
* {@link jakarta.persistence.query.NativeQuery}. An
* {@link NamedStatement} or {@link NamedNativeStatement} annotations,
* or using {@link jakarta.persistence.query.JakartaStatement} or
* {@link jakarta.persistence.query.NativeStatement}. An
* instance of {@code StatementReference} is usually obtained
* from the static metamodel of the annotated type.
*
Expand All @@ -35,7 +35,7 @@
* class Filer {
* @Inject EntityManager entityManager;
*
* @JakartaQuery("delete from Record where temporary = true")
* @JakartaStatement("delete from Record where temporary = true")
* int purgeTemporaryRecords() {
* return entityManager.createStatement(Filer_.purgeTemporaryRecords())
* .execute();
Expand All @@ -44,15 +44,15 @@
* }
*
* <p>In this example, the entity class is annotated,
* and the {@link NamedQuery} annotation explicitly specifies a
* and the {@link NamedStatement} annotation explicitly specifies a
* name:
* {@snippet :
* @NamedQuery(name = "updateSales",
* query = "update Book set sales = ?1 where isbn = ?2")
* @NamedStatement(name = "updateSales",
* statement = "update Book set sales = ?1 where isbn = ?2")
* @Entity class Book { .. }
* }
* <p>In this case the {@code StatementReference} obtained from
* its static metamodel does not include arguments to the query
* its static metamodel does not include arguments to the statement
* parameters, and so they must be supplied via {@code setParameter}:
* {@snippet :
* int updated =
Expand All @@ -63,13 +63,13 @@
* }
*
* <p>A {@code StatementReference} may include arguments to parameters
* of the query.
* of the statement.
* <ul>
* <li>A reference representing a query declared using an
* <li>A reference representing a statement declared using an
* annotation of a type or field has no arguments, and so
* {@link #getArguments()}, {@link #getParameterNames()},
* and {@link #getParameterTypes()} all return {@code null}.
* <li>A reference representing a query declared using an
* <li>A reference representing a statement declared using an
* annotation of a method holds information about the
* arguments passed to the method, making it available via
* {@link #getArguments()}, {@link #getParameterNames()},
Expand All @@ -92,14 +92,14 @@
public non-sealed interface StatementReference extends Reference {
/**
* All {@linkplain Statement.Option options} specified by
* {@link jakarta.persistence.query.QueryOptions},
* {@link NamedQuery}, or {@link NamedNativeQuery}, or an
* {@link jakarta.persistence.query.StatementOptions},
* {@link NamedStatement}, or {@link NamedNativeStatement}, or an
* empty set if no options were specified.
*
* @see Statement#addOption
* @see jakarta.persistence.query.QueryOptions#timeout
* @see jakarta.persistence.query.QueryOptions#flush
* @see jakarta.persistence.query.StatementOptions#timeout
* @see jakarta.persistence.query.StatementOptions#flush
*/
@Nonnull
Set<Statement.Option> getOptions();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,15 @@
* {@link #value} element. The Java type might be:
* <ul>
* <li>an entity class, mapped superclass, or embeddable class,
* <li>a class with one or more {@link jakarta.persistence.NamedQuery}
* or {@link jakarta.persistence.NamedNativeQuery} annotations, or
* <li>a class with one or more {@link jakarta.persistence.NamedQuery},
* {@link jakarta.persistence.NamedNativeQuery},
* {@link jakarta.persistence.NamedStatement}, or
* {@link jakarta.persistence.NamedNativeStatement} annotations, or
* <li>a class with one or more methods annotated
* {@link jakarta.persistence.query.JakartaQuery} or
* {@link jakarta.persistence.query.NativeQuery}.
* {@link jakarta.persistence.query.JakartaQuery},
* {@link jakarta.persistence.query.NativeQuery},
* {@link jakarta.persistence.query.JakartaStatement}, or
* {@link jakarta.persistence.query.NativeStatement}.
* </ul>
*
* <p>Typically, a static metamodel class is generated by an annotation
Expand Down
28 changes: 11 additions & 17 deletions api/src/main/java/jakarta/persistence/query/JakartaQuery.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
import static java.lang.annotation.RetentionPolicy.RUNTIME;

/**
* Specifies a query expressed in the Jakarta Persistence Query Language.
* Specifies a {@code SELECT} query expressed in the Jakarta Persistence
* Query Language. An {@code UPDATE} or {@code DELETE} statement must be
* declared using {@link JakartaStatement}.
* This annotation may be applied to:
* <ul>
* <li>any method of any class or interface belonging to the persistence
Expand Down Expand Up @@ -55,10 +57,6 @@
* where title like = ?1 and pubDate > ?2
* """)
* List<Summary> retrieveSummaries(String title, LocalDate fromDate);
*
* @JakartaQuery("delete from Trash")
* @QueryOptions(timeout = 30_000)
* int emptyTrash();
* }
*}
*
Expand Down Expand Up @@ -87,10 +85,6 @@
* In the second case, each query result is automatically packaged in a new
* instance of the result class by calling the matching constructor.
*
* <p> A method return type <em>agrees</em> with the type returned by an
* {@code UPDATE} or {@code DELETE} statement if the return type is
* {@code void}, {@code int}, or {@code long}.
*
* <p> A method parameter type <em>agrees</em> with a query parameter type if
* it is a type that could be assigned to the corresponding query parameter
* by passing an instance of the type to {@code setParameter()}. In making
Expand Down Expand Up @@ -124,12 +118,6 @@
* em.createQuery(Library_.getBookWithIsbn(isbn))
* .getSingleResult();
* }
* {@snippet :
* int deleted =
* em.createQuery(Library_.emptyTrash())
* .executeUpdate();
* }
*
* <p> In addition, the query is treated as a named query, where the query
* name agrees with the convention for Javadoc links, that is: the
* concatenation of the fully qualified name of the type, with the string
Expand All @@ -138,7 +126,13 @@
* example, {@code "org.example.Library#findBooksByTitle(java.lang.String)"}.
*
* <p> The persistence provider must treat the annotation
* {@code jakarta.data.repository.Query} as a synonym for this annotation.
* {@code jakarta.data.repository.Query} as a synonym for this annotation
* when it specifies a {@code SELECT} query. A method must not be annotated
* with more than one of {@code JakartaQuery}, {@code JakartaStatement},
* {@code NatveQuery}, {@code NativeStatement}, and {@code Query}.
*
* @see JakartaStatement
* @see QueryOptions
*
* @since 4.0
*/
Expand All @@ -149,4 +143,4 @@
* The query string in the Jakarta Persistence Query Language.
*/
String value();
}
}
105 changes: 105 additions & 0 deletions api/src/main/java/jakarta/persistence/query/JakartaStatement.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* Copyright (c) 2026 Contributors to the Eclipse Foundation
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0,
* or the Eclipse Distribution License v. 1.0 which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
*/

// Contributors:
// Gavin King - 4.0

package jakarta.persistence.query;

import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

/**
* Specifies an {@code UPDATE} or {@code DELETE} statement expressed in
* the Jakarta Persistence Query Language. A {@code SELECT} query must be
* declared using {@link JakartaQuery}.
* This annotation may be applied to:
* <ul>
* <li>any method of any class or interface belonging to the persistence
* unit, or
* <li>a query method of a Jakarta Data repository with an implementation
* backed by Jakarta Persistence.
* </ul>
* <p> The return type of the method must be {@code void}, {@code int},
* or {@code long}. The parameter types of the method must agree with the
* types of the statement parameters. An implementation of Jakarta
* Persistence or of Jakarta Data may be able to determine if such
* agreement exists at compilation time or when the persistence unit is
* initialized, but this is not required.
*
* <p>
* {@snippet :
* interface Library {
* @JakartaStatement("delete from Trash")
* @StatementOptions(timeout = 30_000)
* int emptyTrash();
* }
* }
*
* <p> A method parameter type <em>agrees</em> with a statement parameter
* type if it is a type that could be assigned to the corresponding
* statement parameter by passing an instance of the type to
* {@code setParameter()}. In making this determination, method parameters
* are correlated with statement parameters by position or by name,
* depending on whether the statement uses positional or named parameters.
*
* <p> The parameter types of the annotated method must be basic types.
* If this annotation occurs on a generic method, on a method with a
* parameter type which is not a basic type, or on a method with a
* parameter type involving a type variable, the behavior is undefined.
*
* <p> Any implementation of Jakarta Data backed by Jakarta Persistence
* must support the use of this annotation as a Jakarta Data query
* annotation. Thus, a repository method annotated {@code @JakartaStatement}
* is treated by the Jakarta Data provider as a query method, and an
* implementation of the method is supplied by the Jakarta Data provider.
*
* <p> When this annotation is applied to a method of a class or interface
* belonging to the persistence unit, a reference to a statement declared
* using this annotation may be obtained from the static metamodel class
* of the annotated class or interface.
* {@snippet :
* int deleted =
* em.createStatement(Library_.emptyTrash())
* .execute();
* }
*
* <p> In addition, the statement is treated as a named statement, where
* the statement name agrees with the convention for Javadoc links, that
* is: the concatenation of the fully qualified name of the type, with
* the string {@code #}, the name of the annotated member, and the raw
* types of the method parameters, enclosed in parentheses and separated
* by commas, for example, {@code "org.example.Library#emptyTrash()"}.
*
* <p> The persistence provider must treat the annotation
* {@code jakarta.data.repository.Query} as a synonym for this annotation
* when it specifies an {@code UPDATE} or {@code DELETE} statement. A
* method must not be annotated with more than one of {@code JakartaQuery},
* {@code JakartaStatement}, {@code NatveQuery}, {@code NativeStatement},
* and {@code Query}.
*
* @see JakartaQuery
* @see StatementOptions
*
* @since 4.0
*/
@Target(METHOD)
@Retention(RUNTIME)
public @interface JakartaStatement {
Comment thread
gavinking marked this conversation as resolved.
Resolved
/**
* The statement string in the Jakarta Persistence Query Language.
*/
String value();
}
Loading