diff --git a/hibernate-core/src/main/java/org/hibernate/query/Query.java b/hibernate-core/src/main/java/org/hibernate/query/Query.java index 7a52aedc18..9d48abad1e 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/Query.java +++ b/hibernate-core/src/main/java/org/hibernate/query/Query.java @@ -6,12 +6,18 @@ */ package org.hibernate.query; +import java.io.Serializable; +import java.math.BigDecimal; +import java.math.BigInteger; import java.time.Instant; import java.time.LocalDateTime; import java.time.OffsetDateTime; import java.time.ZonedDateTime; import java.util.Calendar; +import java.util.Collection; import java.util.Date; +import java.util.Locale; +import java.util.Map; import java.util.Optional; import java.util.stream.Stream; import javax.persistence.FlushModeType; @@ -26,6 +32,24 @@ import org.hibernate.Incubating; import org.hibernate.LockMode; import org.hibernate.LockOptions; import org.hibernate.engine.spi.RowSelection; +import org.hibernate.transform.ResultTransformer; +import org.hibernate.type.BigDecimalType; +import org.hibernate.type.BigIntegerType; +import org.hibernate.type.BinaryType; +import org.hibernate.type.BooleanType; +import org.hibernate.type.ByteType; +import org.hibernate.type.CharacterType; +import org.hibernate.type.DateType; +import org.hibernate.type.DoubleType; +import org.hibernate.type.FloatType; +import org.hibernate.type.IntegerType; +import org.hibernate.type.LocaleType; +import org.hibernate.type.LongType; +import org.hibernate.type.ShortType; +import org.hibernate.type.StringType; +import org.hibernate.type.TextType; +import org.hibernate.type.TimeType; +import org.hibernate.type.TimestampType; import org.hibernate.type.Type; /** @@ -98,7 +122,6 @@ public interface Query extends TypedQuery, org.hibernate.Query, CommonQ Query setParameter(int position, OffsetDateTime value, TemporalType temporalType); - // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // covariant overrides @@ -142,6 +165,24 @@ public interface Query extends TypedQuery, org.hibernate.Query, CommonQ @Override Query setParameter(int position, Date value, TemporalType temporalType); + @Override + Query setParameter(QueryParameter parameter, T val); + + @Override +

Query setParameter(int position, P val, TemporalType temporalType); + + @Override +

Query setParameter(QueryParameter

parameter, P val, Type type); + + @Override + Query setParameter(int position, Object val, Type type); + + @Override +

Query setParameter(QueryParameter

parameter, P val, TemporalType temporalType); + + @Override +

Query setParameter(String name, P val, TemporalType temporalType); + @Override Query setFlushMode(FlushModeType flushMode); @@ -154,12 +195,6 @@ public interface Query extends TypedQuery, org.hibernate.Query, CommonQ @Override Query setHibernateFlushMode(FlushMode flushMode); - @Override - default Query setFlushMode(FlushMode flushMode) { - setHibernateFlushMode( flushMode ); - return this; - } - @Override Query setCacheMode(CacheMode cacheMode); @@ -186,4 +221,837 @@ public interface Query extends TypedQuery, org.hibernate.Query, CommonQ @Override Query addQueryHint(String hint); + + @Override +

Query setParameterList(QueryParameter

parameter, Collection

values); + + @Override + Query setParameterList(String name, Collection values); + + @Override + Query setParameterList(String name, Collection values, Type type); + + @Override + Query setParameterList(String name, Object[] values, Type type); + + @Override + Query setParameterList(String name, Object[] values); + + @Override + Query setProperties(Object bean); + + @Override + Query setProperties(Map bean); + + + // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + // deprecations + + /** + * (Re)set the current FlushMode in effect for this query. + * + * @param flushMode The new FlushMode to use. + * + * @return {@code this}, for method chaining + * + * @see #getHibernateFlushMode() + * + * @deprecated (since 5.2) use {@link #setHibernateFlushMode} instead + */ + @Override + @Deprecated + default Query setFlushMode(FlushMode flushMode) { + setHibernateFlushMode( flushMode ); + return this; + } + + /** + * Bind a positional String-valued parameter. + * + * @param position The parameter position + * @param val The bind value + * + * @return {@code this}, for method chaining + * + * @deprecated (since 5.2) use {@link #setParameter(int, Object)} or {@link #setParameter(int, Object, Type)} + * instead + */ + @Deprecated + @SuppressWarnings("unchecked") + default Query setString(int position, String val) { + setParameter( position, val, StringType.INSTANCE ); + return this; + } + + /** + * Bind a positional char-valued parameter. + * + * @param position The parameter position + * @param val The bind value + * + * @return {@code this}, for method chaining + * + * @deprecated (since 5.2) use {@link #setParameter(int, Object)} or {@link #setParameter(int, Object, Type)} + * instead + */ + @Deprecated + @SuppressWarnings("unchecked") + default Query setCharacter(int position, char val) { + setParameter( position, val, CharacterType.INSTANCE ); + return this; + } + + /** + * Bind a positional boolean-valued parameter. + * + * @param position The parameter position + * @param val The bind value + * + * @return {@code this}, for method chaining + * + * @deprecated (since 5.2) use {@link #setParameter(int, Object)} or {@link #setParameter(int, Object, Type)} + * instead + */ + @Deprecated + @SuppressWarnings("unchecked") + default Query setBoolean(int position, boolean val) { + setParameter( position, val, determineProperBooleanType( position, val, BooleanType.INSTANCE ) ); + return this; + } + + /** + * Bind a positional byte-valued parameter. + * + * @param position The parameter position + * @param val The bind value + * + * @return {@code this}, for method chaining + * + * @deprecated (since 5.2) use {@link #setParameter(int, Object)} or {@link #setParameter(int, Object, Type)} + * instead + */ + @Deprecated + @SuppressWarnings("unchecked") + default Query setByte(int position, byte val) { + setParameter( position, val, ByteType.INSTANCE ); + return this; + } + + /** + * Bind a positional short-valued parameter. + * + * @param position The parameter position + * @param val The bind value + * + * @return {@code this}, for method chaining + * + * @deprecated (since 5.2) use {@link #setParameter(int, Object)} or {@link #setParameter(int, Object, Type)} + * instead + */ + @Deprecated + @SuppressWarnings("unchecked") + default Query setShort(int position, short val) { + setParameter( position, val, ShortType.INSTANCE ); + return this; + } + + /** + * Bind a positional int-valued parameter. + * + * @param position The parameter position + * @param val The bind value + * + * @return {@code this}, for method chaining + * + * @deprecated (since 5.2) use {@link #setParameter(int, Object)} or {@link #setParameter(int, Object, Type)} + * instead + */ + @Deprecated + @SuppressWarnings("unchecked") + default Query setInteger(int position, int val) { + setParameter( position, val, IntegerType.INSTANCE ); + return this; + } + + /** + * Bind a positional long-valued parameter. + * + * @param position The parameter position + * @param val The bind value + * + * @return {@code this}, for method chaining + * + * @deprecated (since 5.2) use {@link #setParameter(int, Object)} or {@link #setParameter(int, Object, Type)} + * instead + */ + @Deprecated + @SuppressWarnings("unchecked") + default Query setLong(int position, long val) { + setParameter( position, val, LongType.INSTANCE ); + return this; + } + + /** + * Bind a positional float-valued parameter. + * + * @param position The parameter position + * @param val The bind value + * + * @return {@code this}, for method chaining + * + * @deprecated (since 5.2) use {@link #setParameter(int, Object)} or {@link #setParameter(int, Object, Type)} + * instead + */ + @Deprecated + @SuppressWarnings("unchecked") + default Query setFloat(int position, float val) { + setParameter( position, val, FloatType.INSTANCE ); + return this; + } + + /** + * Bind a positional double-valued parameter. + * + * @param position The parameter position + * @param val The bind value + * + * @return {@code this}, for method chaining + * + * @deprecated (since 5.2) use {@link #setParameter(int, Object)} or {@link #setParameter(int, Object, Type)} + * instead + */ + @Deprecated + @SuppressWarnings("unchecked") + default Query setDouble(int position, double val) { + setParameter( position, val, DoubleType.INSTANCE ); + return this; + } + + /** + * Bind a positional binary-valued parameter. + * + * @param position The parameter position + * @param val The bind value + * + * @return {@code this}, for method chaining + * + * @deprecated (since 5.2) use {@link #setParameter(int, Object)} or {@link #setParameter(int, Object, Type)} + * instead + */ + @Deprecated + @SuppressWarnings("unchecked") + default Query setBinary(int position, byte[] val) { + setParameter( position, val, BinaryType.INSTANCE ); + return this; + } + + /** + * Bind a positional String-valued parameter using streaming. + * + * @param position The parameter position + * @param val The bind value + * + * @return {@code this}, for method chaining + * + * @deprecated (since 5.2) use {@link #setParameter(int, Object)} or {@link #setParameter(int, Object, Type)} + * instead + */ + @Deprecated + @SuppressWarnings("unchecked") + default Query setText(int position, String val) { + setParameter( position, val, TextType.INSTANCE ); + return this; + } + + /** + * Bind a positional binary-valued parameter using serialization. + * + * @param position The parameter position + * @param val The bind value + * + * @return {@code this}, for method chaining + * + * @deprecated (since 5.2) use {@link #setParameter(int, Object)} or {@link #setParameter(int, Object, Type)} + * instead + */ + @Deprecated + @SuppressWarnings("unchecked") + default Query setSerializable(int position, Serializable val) { + setParameter( position, val ); + return this; + } + + /** + * Bind a positional Locale-valued parameter. + * + * @param position The parameter position + * @param val The bind value + * + * @return {@code this}, for method chaining + * + * @deprecated (since 5.2) use {@link #setParameter(int, Object)} or {@link #setParameter(int, Object, Type)} + * instead + */ + @Deprecated + @SuppressWarnings("unchecked") + default Query setLocale(int position, Locale val) { + setParameter( position, val, LocaleType.INSTANCE ); + return this; + } + + /** + * Bind a positional BigDecimal-valued parameter. + * + * @param position The parameter position + * @param val The bind value + * + * @return {@code this}, for method chaining + * + * @deprecated (since 5.2) use {@link #setParameter(int, Object)} or {@link #setParameter(int, Object, Type)} + * instead + */ + @Deprecated + @SuppressWarnings("unchecked") + default Query setBigDecimal(int position, BigDecimal val) { + setParameter( position, val, BigDecimalType.INSTANCE ); + return this; + } + + /** + * Bind a positional BigDecimal-valued parameter. + * + * @param position The parameter position + * @param val The bind value + * + * @return {@code this}, for method chaining + * + * @deprecated (since 5.2) use {@link #setParameter(int, Object)} or {@link #setParameter(int, Object, Type)} + * instead + */ + @Deprecated + @SuppressWarnings("unchecked") + default Query setBigInteger(int position, BigInteger val) { + setParameter( position, val, BigIntegerType.INSTANCE ); + return this; + } + + /** + * Bind a positional Date-valued parameter using just the Date portion. + * + * @param position The parameter position + * @param val The bind value + * + * @return {@code this}, for method chaining + * + * @deprecated (since 5.2) use {@link #setParameter(int, Object)} or {@link #setParameter(int, Object, Type)} + * instead + */ + @Deprecated + @SuppressWarnings("unchecked") + default Query setDate(int position, Date val) { + setParameter( position, val, DateType.INSTANCE ); + return this; + } + + /** + * Bind a positional Date-valued parameter using just the Time portion. + * + * @param position The parameter position + * @param val The bind value + * + * @return {@code this}, for method chaining + * + * @deprecated (since 5.2) use {@link #setParameter(int, Object)} or {@link #setParameter(int, Object, Type)} + * instead + */ + @Deprecated + @SuppressWarnings("unchecked") + default Query setTime(int position, Date val) { + setParameter( position, val, TimeType.INSTANCE ); + return this; + } + + /** + * Bind a positional Date-valued parameter using the full Timestamp. + * + * @param position The parameter position + * @param val The bind value + * + * @return {@code this}, for method chaining + * + * @deprecated (since 5.2) use {@link #setParameter(int, Object)} or {@link #setParameter(int, Object, Type)} + * instead + */ + @Deprecated + @SuppressWarnings("unchecked") + default Query setTimestamp(int position, Date val) { + setParameter( position, val, TimestampType.INSTANCE ); + return this; + } + + /** + * Bind a positional Calendar-valued parameter using the full Timestamp portion. + * + * @param position The parameter position + * @param val The bind value + * + * @return {@code this}, for method chaining + * + * @deprecated (since 5.2) use {@link #setParameter(int, Object)} or {@link #setParameter(int, Object, Type)} + * instead + */ + @Deprecated + @SuppressWarnings("unchecked") + default Query setCalendar(int position, Calendar val) { + setParameter( position, val, TimestampType.INSTANCE ); + return this; + } + + /** + * Bind a positional Calendar-valued parameter using just the Date portion. + * + * @param position The parameter position + * @param val The bind value + * + * @return {@code this}, for method chaining + * + * @deprecated (since 5.2) use {@link #setParameter(int, Object)} or {@link #setParameter(int, Object, Type)} + * instead + */ + @Deprecated + @SuppressWarnings("unchecked") + default Query setCalendarDate(int position, Calendar val) { + setParameter( position, val, DateType.INSTANCE ); + return this; + } + + /** + * Bind a named String-valued parameter. + * + * @param name The parameter name + * @param val The bind value + * + * @return {@code this}, for method chaining + * + * @deprecated (since 5.2) use {@link #setParameter(int, Object)} or {@link #setParameter(int, Object, Type)} + * instead + */ + @Deprecated + @SuppressWarnings("unchecked") + default Query setString(String name, String val) { + setParameter( name, val, StringType.INSTANCE ); + return this; + } + + /** + * Bind a named char-valued parameter. + * + * @param name The parameter name + * @param val The bind value + * + * @return {@code this}, for method chaining + * + * @deprecated (since 5.2) use {@link #setParameter(int, Object)} or {@link #setParameter(int, Object, Type)} + * instead + */ + @Deprecated + @SuppressWarnings("unchecked") + default Query setCharacter(String name, char val) { + setParameter( name, val, CharacterType.INSTANCE ); + return this; + } + + /** + * Bind a named boolean-valued parameter. + * + * @param name The parameter name + * @param val The bind value + * + * @return {@code this}, for method chaining + * + * @deprecated (since 5.2) use {@link #setParameter(int, Object)} or {@link #setParameter(int, Object, Type)} + * instead + */ + @Deprecated + @SuppressWarnings("unchecked") + default Query setBoolean(String name, boolean val) { + setParameter( name, val, determineProperBooleanType( name, val, BooleanType.INSTANCE ) ); + return this; + } + + /** + * Bind a named byte-valued parameter. + * + * @param name The parameter name + * @param val The bind value + * + * @return {@code this}, for method chaining + * + * @deprecated (since 5.2) use {@link #setParameter(int, Object)} or {@link #setParameter(int, Object, Type)} + * instead + */ + @Deprecated + @SuppressWarnings("unchecked") + default Query setByte(String name, byte val) { + setParameter( name, val, ByteType.INSTANCE ); + return this; + } + + /** + * Bind a named short-valued parameter. + * + * @param name The parameter name + * @param val The bind value + * + * @return {@code this}, for method chaining + * + * @deprecated (since 5.2) use {@link #setParameter(int, Object)} or {@link #setParameter(int, Object, Type)} + * instead + */ + @Deprecated + @SuppressWarnings("unchecked") + default Query setShort(String name, short val) { + setParameter( name, val, ShortType.INSTANCE ); + return this; + } + + /** + * Bind a named int-valued parameter. + * + * @param name The parameter name + * @param val The bind value + * + * @return {@code this}, for method chaining + * + * @deprecated (since 5.2) use {@link #setParameter(int, Object)} or {@link #setParameter(int, Object, Type)} + * instead + */ + @Deprecated + @SuppressWarnings("unchecked") + default Query setInteger(String name, int val) { + setParameter( name, val, IntegerType.INSTANCE ); + return this; + } + + /** + * Bind a named long-valued parameter. + * + * @param name The parameter name + * @param val The bind value + * + * @return {@code this}, for method chaining + * + * @deprecated (since 5.2) use {@link #setParameter(int, Object)} or {@link #setParameter(int, Object, Type)} + * instead + */ + @Deprecated + @SuppressWarnings("unchecked") + default Query setLong(String name, long val) { + setParameter( name, val, LongType.INSTANCE ); + return this; + } + + /** + * Bind a named float-valued parameter. + * + * @param name The parameter name + * @param val The bind value + * + * @return {@code this}, for method chaining + * + * @deprecated (since 5.2) use {@link #setParameter(int, Object)} or {@link #setParameter(int, Object, Type)} + * instead + */ + @Deprecated + @SuppressWarnings("unchecked") + default Query setFloat(String name, float val) { + setParameter( name, val, FloatType.INSTANCE ); + return this; + } + + /** + * Bind a named double-valued parameter. + * + * @param name The parameter name + * @param val The bind value + * + * @return {@code this}, for method chaining + * + * @deprecated (since 5.2) use {@link #setParameter(int, Object)} or {@link #setParameter(int, Object, Type)} + * instead + */ + @Deprecated + @SuppressWarnings("unchecked") + default Query setDouble(String name, double val) { + setParameter( name, val, DoubleType.INSTANCE ); + return this; + } + + /** + * Bind a named binary-valued parameter. + * + * @param name The parameter name + * @param val The bind value + * + * @return {@code this}, for method chaining + * + * @deprecated (since 5.2) use {@link #setParameter(int, Object)} or {@link #setParameter(int, Object, Type)} + * instead + */ + @Deprecated + @SuppressWarnings("unchecked") + default Query setBinary(String name, byte[] val) { + setParameter( name, val, BinaryType.INSTANCE ); + return this; + } + + /** + * Bind a named String-valued parameter using streaming. + * + * @param name The parameter name + * @param val The bind value + * + * @return {@code this}, for method chaining + * + * @deprecated (since 5.2) use {@link #setParameter(int, Object)} or {@link #setParameter(int, Object, Type)} + * instead + */ + @Deprecated + @SuppressWarnings("unchecked") + default Query setText(String name, String val) { + setParameter( name, val, TextType.INSTANCE ); + return this; + } + + /** + * Bind a named binary-valued parameter using serialization. + * + * @param name The parameter name + * @param val The bind value + * + * @return {@code this}, for method chaining + * + * @deprecated (since 5.2) use {@link #setParameter(int, Object)} or {@link #setParameter(int, Object, Type)} + * instead + */ + @Deprecated + @SuppressWarnings("unchecked") + default Query setSerializable(String name, Serializable val) { + setParameter( name, val ); + return this; + } + + /** + * Bind a named Locale-valued parameter. + * + * @param name The parameter name + * @param val The bind value + * + * @return {@code this}, for method chaining + * @deprecated (since 5.2) use {@link #setParameter(int, Object)} or {@link #setParameter(int, Object, Type)} + * instead + */ + @Deprecated + @SuppressWarnings("unchecked") + default Query setLocale(String name, Locale val) { + setParameter( name, val, TextType.INSTANCE ); + return this; + } + + /** + * Bind a named BigDecimal-valued parameter. + * + * @param name The parameter name + * @param val The bind value + * + * @return {@code this}, for method chaining + * @deprecated (since 5.2) use {@link #setParameter(int, Object)} or {@link #setParameter(int, Object, Type)} + * instead + */ + @Deprecated + @SuppressWarnings("unchecked") + default Query setBigDecimal(String name, BigDecimal val) { + setParameter( name, val, BigDecimalType.INSTANCE ); + return this; + } + + /** + * Bind a named BigInteger-valued parameter. + * + * @param name The parameter name + * @param val The bind value + * + * @return {@code this}, for method chaining + * @deprecated (since 5.2) use {@link #setParameter(int, Object)} or {@link #setParameter(int, Object, Type)} + * instead + */ + @Deprecated + @SuppressWarnings("unchecked") + default Query setBigInteger(String name, BigInteger val) { + setParameter( name, val, BigIntegerType.INSTANCE ); + return this; + } + + /** + * Bind the val (time is truncated) of a given Date object to a named query parameter. + * + * @param name The name of the parameter + * @param val The val object + * + * @return {@code this}, for method chaining + * + * @deprecated (since 5.2) use {@link #setParameter(int, Object)} or {@link #setParameter(int, Object, Type)} + * instead + */ + @Deprecated + @SuppressWarnings("unchecked") + default Query setDate(String name, Date val) { + setParameter( name, val, DateType.INSTANCE ); + return this; + } + + /** + * Bind the time (val is truncated) of a given Date object to a named query parameter. + * + * @param name The name of the parameter + * @param val The val object + * + * @return {@code this}, for method chaining + * + * @deprecated (since 5.2) use {@link #setParameter(int, Object)} or {@link #setParameter(int, Object, Type)} + * instead + */ + @Deprecated + @SuppressWarnings("unchecked") + default Query setTime(String name, Date val) { + setParameter( name, val, TimeType.INSTANCE ); + return this; + } + + /** + * Bind the value and the time of a given Date object to a named query parameter. + * + * @param name The name of the parameter + * @param value The value object + * + * @return {@code this}, for method chaining + * + * @deprecated (since 5.2) use {@link #setParameter(int, Object)} or {@link #setParameter(int, Object, Type)} + * instead + */ + @Deprecated + @SuppressWarnings("unchecked") + default Query setTimestamp(String name, Date value) { + setParameter( name, value, TimestampType.INSTANCE ); + return this; + } + + /** + * Bind a named Calendar-valued parameter using the full Timestamp. + * + * @param name The parameter name + * @param value The bind value + * + * @return {@code this}, for method chaining + * + * @deprecated (since 5.2) use {@link #setParameter(int, Object)} or {@link #setParameter(int, Object, Type)} + * instead + */ + @Deprecated + @SuppressWarnings("unchecked") + default Query setCalendar(String name, Calendar value) { + setParameter( name, value, TimestampType.INSTANCE ); + return this; + } + + /** + * Bind a named Calendar-valued parameter using just the Date portion. + * + * @param name The parameter name + * @param value The bind value + * + * @return {@code this}, for method chaining + * + * @deprecated (since 5.2) use {@link #setParameter(int, Object)} or {@link #setParameter(int, Object, Type)} + * instead + */ + @Deprecated + @SuppressWarnings("unchecked") + default Query setCalendarDate(String name, Calendar value) { + setParameter( name, value, DateType.INSTANCE ); + return this; + } + + /** + * Bind an instance of a mapped persistent class to a JDBC-style query parameter. + * Use {@link #setParameter(int, Object)} for null values. + * + * @param position the position of the parameter in the query + * string, numbered from 0. + * @param val a non-null instance of a persistent class + * + * @return {@code this}, for method chaining + * + * @deprecated (since 5.2) use {@link #setParameter(int, Object)} or {@link #setParameter(int, Object, Type)} + * instead + */ + @Deprecated + @SuppressWarnings("unchecked") + Query setEntity(int position, Object val); + + /** + * Bind an instance of a mapped persistent class to a named query parameter. Use + * {@link #setParameter(String, Object)} for null values. + * + * @param name the name of the parameter + * @param val a non-null instance of a persistent class + * + * @return {@code this}, for method chaining + * + * @deprecated (since 5.2) use {@link #setParameter(int, Object)} or {@link #setParameter(int, Object, Type)} + * instead + */ + @Deprecated + @SuppressWarnings("unchecked") + Query setEntity(String name, Object val); + + /** + * Set a strategy for handling the query results. This can be used to change + * "shape" of the query result. + * + * @param transformer The transformer to apply + * + * @return this (for method chaining) + * + * @deprecated (since 5.2) + * @todo develop a new approach to result transformers + */ + @Deprecated + Query setResultTransformer(ResultTransformer transformer); + + /** + * Bind values and types to positional parameters. Allows binding more than one at a time; no real performance + * impact. + * + * The number of elements in each array should match. That is, element number-0 in types array corresponds to + * element-0 in the values array, etc, + * + * @param types The types + * @param values The values + * + * @return {@code this}, for method chaining + * + * @deprecated (since 5.2) Bind values individually + */ + @Deprecated + @SuppressWarnings("unchecked") + default Query setParameters(Object[] values, Type[] types) { + assert values.length == types.length; + for ( int i = 0; i < values.length; i++ ) { + setParameter( i, values[i], types[i] ); + } + + return this; + } + }