Skip to content
Open
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
42 changes: 42 additions & 0 deletions api/src/main/java/jakarta/persistence/ArgumentRecord.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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
*/
package jakarta.persistence;

import jakarta.annotation.Nonnull;

import static java.util.Objects.requireNonNull;

/**
* Internal implementation of {@link Parameter.Argument}.
* @param <T> the type of the parameter
* @param parameter the parameter
* @param value the argument bound to the parameter
* @since 4.0
*/
record ArgumentRecord<T>(@Nonnull Parameter<T> parameter, T value)
implements Parameter.Argument<T> {

ArgumentRecord {
requireNonNull(parameter, "parameter must not be null");
}

@Nonnull
@Override
public Parameter<T> getParameter() {
return parameter;
}

@Override
public T getValue() {
return value;
}
}
50 changes: 50 additions & 0 deletions api/src/main/java/jakarta/persistence/Parameter.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,54 @@ public interface Parameter<T> {
*/
@Nonnull
Class<T> getParameterType();

/**
* Create a new instance with the given type, position, and name.
* @param type the Java type of the parameter
* @param position the position of the parameter, or null if the
* parameter is not a positional parameter
* @param name the name of the parameter, or null if the parameter
* is not a named parameter
* @return a new instance
* @param <T> the type of the parameter
* @since 4.0
*/
@Nonnull
static <T> Parameter<T> of(@Nonnull Class<T> type,
@Nullable Integer position,
@Nullable String name) {
return new ParameterRecord<>(type, position, name);
}

/**
* Packages a parameter with its argument.
* @param <T> the type of the parameter
* @since 4.0
*/
interface Argument<T> {
/**
* The parameter.
* @return the parameter
*/
@Nonnull
Parameter<T> getParameter();

/**
* The argument bound to the parameter.
* @return the argument value
*/
T getValue();
}

/**
* Return a new {@link Argument} binding this parameter
* to the given argument value.
* @param argument the argument bound to this parameter
* @return a tuple entry
* @since 4.0
*/
@Nonnull
default Argument<T> bind(T argument) {
return new ArgumentRecord<>(this, argument);
}
}
48 changes: 48 additions & 0 deletions api/src/main/java/jakarta/persistence/ParameterRecord.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* 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
*/
package jakarta.persistence;
Comment thread
gavinking marked this conversation as resolved.
Resolved

import jakarta.annotation.Nonnull;
import jakarta.annotation.Nullable;

/**
* Internal implementation of {@link Parameter}.
* @param type the Java type of the parameter
* @param position the position of the parameter, or null if the
* parameter is not a positional parameter
* @param name the name of the parameter, or null if the parameter
* is not a named parameter
* @param <T> the type of the parameter
* @see Parameter#of(Class, Integer, String)
* @since 4.0
*/
record ParameterRecord<T>
(@Nonnull Class<T> type, @Nullable Integer position, @Nullable String name)
implements Parameter<T> {
@Nullable
@Override
public String getName() {
return name;
}

@Nullable
@Override
public Integer getPosition() {
return position;
}

@Nonnull
@Override
public Class<T> getParameterType() {
return type;
}
}
25 changes: 25 additions & 0 deletions api/src/main/java/jakarta/persistence/Query.java
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,31 @@ Query setParameter(int position, @Nullable Date value,
@Nonnull
Query setParameters(@Nonnull Object... arguments);

/**
* Bind an argument to a parameter of the query.
* @param argument The argument to bind
* @return the same query instance
* @throws IllegalArgumentException if one of arguments does
* not match a parameter of the query
* @since 4.0
*/
@Nonnull
Query setArgument(@Nonnull Parameter.Argument<?> argument);

/**
* Bind arguments to the parameters of the query.
* @param arguments The arguments to bind
* @return the same query instance
* @throws IllegalArgumentException if the number of arguments
* is not exactly the same as the number of parameters,
* if one of arguments does not match a parameter of
* the query, or if multiple arguments are provided
* for the same parameter
* @since 4.0
*/
@Nonnull
Query setArguments(@Nonnull List<? extends Parameter.Argument<?>> arguments);

/**
* Get the {@link Parameter} objects representing the declared
* parameters of the query or an empty set if the query has no
Expand Down
39 changes: 31 additions & 8 deletions api/src/main/java/jakarta/persistence/Reference.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ public sealed interface Reference
* to a statement or query declared using an annotation of a
* method.
* <p>
* Any mutation of the returned list results in an
* {@link UnsupportedOperationException}.
* Mutation of the returned list does not affect the reference.
*
* @since 4.0
*/
Expand All @@ -77,8 +76,7 @@ public sealed interface Reference
* interpreted as the parameter names. Otherwise, if the
* query has positional parameters, they are ignored.
* <p>
* Any mutation of the returned list results in an
* {@link UnsupportedOperationException}.
* Mutation of the returned list does not affect the reference.
*
* @since 4.0
*/
Expand All @@ -100,8 +98,7 @@ public sealed interface Reference
* obtain an assignment of arguments to parameters.
* </ul>
* <p>
* Any mutation of the returned list results in an
* {@link UnsupportedOperationException}.
* Mutation of the returned list does not affect the reference.
*
* @see Query#setParameter(int, Object)
* @see Query#setParameter(String, Object)
Expand All @@ -110,15 +107,41 @@ public sealed interface Reference
@Nullable
List<Object> getArguments();

/**
* The parameters of the statement or query when arguments
* to the parameters were supplied, or {@code null} if no
* arguments were supplied. Arguments are present when this
* is a reference to a statement or query declared using an
* annotation of a method.
* <p>
* Mutation of the returned list does not affect the reference.
*
* @since 4.0
*/
@Nullable
List<Parameter<?>> getParameters();

/**
* The arguments supplied to the parameters of the statement
* or query, or {@code null} if no arguments were supplied.
* Arguments are present when this is a reference to a query
* declared using an annotation of a method.
*
* @see Query#setArgument(Parameter.Argument)
*
* @since 4.0
*/
@Nullable
List<Parameter.Argument<?>> getParameterArguments();

/**
* A map keyed by hint name of all hints specified via
* {@link NamedQuery#hints}, {@link NamedNativeQuery#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}.
* Mutation of the returned map does not affect the reference.
*/
@Nonnull
Map<String,Object> getHints();
Expand Down
9 changes: 9 additions & 0 deletions api/src/main/java/jakarta/persistence/Statement.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
Expand Down Expand Up @@ -494,6 +495,14 @@ Statement setParameter(int position, @Nullable Date value,
@Override
Statement setParameters(@Nonnull Object... arguments);

@Nonnull
@Override
Statement setArgument(@Nonnull Parameter.Argument<?> argument);

@Nonnull
@Override
Statement setArguments(@Nonnull List<? extends Parameter.Argument<?>> arguments);

/**
* Specify an {@linkplain Option option} influencing execution
* of this statement, overwriting any existing option of the
Expand Down
10 changes: 10 additions & 0 deletions api/src/main/java/jakarta/persistence/StatementOrTypedQuery.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import jakarta.persistence.metamodel.Type;
import jakarta.persistence.sql.ResultSetMapping;

import java.util.List;

/**
* Declares operations allowing a {@link Statement} or {@link TypedQuery}
* to be obtained when the type of a query or statement has not yet been
Expand Down Expand Up @@ -184,6 +186,14 @@ <P> StatementOrTypedQuery setConvertedParameter(int position, @Nullable P value,
@Override
StatementOrTypedQuery setParameters(@Nonnull Object... arguments);

@Nonnull
@Override
StatementOrTypedQuery setArgument(@Nonnull Parameter.Argument<?> argument);

@Nonnull
@Override
StatementOrTypedQuery setArguments(@Nonnull List<? extends Parameter.Argument<?>> arguments);

@Nonnull
@Override
StatementOrTypedQuery setQueryFlushMode(@Nonnull QueryFlushMode flushMode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,14 @@ <P> StoredProcedureQuery setConvertedParameter(int position, @Nullable P value,
@Override
StoredProcedureQuery setParameters(@Nonnull Object... arguments);

@Nonnull
@Override
StoredProcedureQuery setArgument(@Nonnull Parameter.Argument<?> argument);

@Nonnull
@Override
StoredProcedureQuery setArguments(@Nonnull List<? extends Parameter.Argument<?>> arguments);

/**
* Set the {@linkplain QueryFlushMode query flush mode} to be
* used when the query is executed. This flush mode overrides
Expand Down
56 changes: 53 additions & 3 deletions api/src/main/java/jakarta/persistence/Tuple.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@

import java.util.List;

import static java.util.Arrays.asList;

/**
* Interface for extracting the elements of a query result tuple.
* A {@code Tuple} may be used to represent a query result row
* containing multiple projected values.
* <p>
* This example uses a criteria query to select the title and
* publication date of each book, and then reuses the projected
* publication date of each book and then reuses the projected
* {@link TupleElement}s to extract the results:
* {@snippet :
* var builder = factory.getCriteriaBuilder();
Expand Down Expand Up @@ -117,8 +119,7 @@ public interface Tuple {
Object get(int i);

/**
* Return the values of the result tuple elements as
* an array.
* Return the tuple element values as an array.
* @return tuple element values
*/
@Nonnull
Expand All @@ -130,4 +131,53 @@ public interface Tuple {
*/
@Nonnull
List<TupleElement<?>> getElements();

/**
* Return the number of elements of this tuple.
* @return length of tuple
* @since 4.0
*/
default int length() {
return getElements().size();
}

/**
* Return the tuple element values as a list.
* @return tuple element values
* @since 4.0
*/
@Nonnull
default List<Object> getValues() {
return asList(toArray());
}

/**
* Return the tuple element types.
* @return tuple element types
* @since 4.0
*/
@Nonnull
default List<Class<?>> getElementTypes() {
final var elements = getElements();
final var types = new Class<?>[elements.size()];
for (int i = 0; i < elements.size(); i++) {
types[i] = elements.get(i).getJavaType();
}
return asList(types);
}

/**
* Return the tuple element aliases.
* @return tuple element aliases
* @since 4.0
*/
@Nonnull
default List<String> getAliases() {
final var elements = getElements();
final var aliases = new String[elements.size()];
for (int i = 0; i < elements.size(); i++) {
aliases[i] = elements.get(i).getAlias();
}
return asList(aliases);
}
}
Loading