diff --git a/hibernate-core/src/main/java/org/hibernate/engine/spi/SessionDelegatorBaseImpl.java b/hibernate-core/src/main/java/org/hibernate/engine/spi/SessionDelegatorBaseImpl.java index 0d97bcd79e..d14f4c21ba 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/spi/SessionDelegatorBaseImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/spi/SessionDelegatorBaseImpl.java @@ -50,6 +50,8 @@ import org.hibernate.jdbc.ReturningWork; import org.hibernate.jdbc.Work; import org.hibernate.persister.entity.EntityPersister; import org.hibernate.procedure.ProcedureCall; +import org.hibernate.query.NativeQuery; +import org.hibernate.query.Query; import org.hibernate.query.criteria.HibernateCriteriaBuilder; import org.hibernate.query.spi.QueryImplementor; import org.hibernate.query.spi.QueryProducerImplementor; @@ -526,6 +528,21 @@ public class SessionDelegatorBaseImpl implements SessionImplementor { return queryDelegate().createNativeQuery( sqlString, resultSetMappingName, resultClass ); } + @Override + public QueryImplementor createStatement(String statementString) { + return delegate.createStatement( statementString ); + } + + @Override + public QueryImplementor createNamedStatement(String name) { + return delegate.createNamedStatement( name ); + } + + @Override + public NativeQueryImplementor createNativeStatement(String sqlString) { + return delegate.createNativeStatement( sqlString ); + } + @Override public ProcedureCall createNamedStoredProcedureQuery(String name) { return delegate.createNamedStoredProcedureQuery( name ); diff --git a/hibernate-core/src/main/java/org/hibernate/internal/AbstractSharedSessionContract.java b/hibernate-core/src/main/java/org/hibernate/internal/AbstractSharedSessionContract.java index 44673a9e0c..38a7671a5e 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/AbstractSharedSessionContract.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/AbstractSharedSessionContract.java @@ -57,6 +57,7 @@ import org.hibernate.persister.entity.EntityPersister; import org.hibernate.procedure.ProcedureCall; import org.hibernate.procedure.internal.ProcedureCallImpl; import org.hibernate.procedure.spi.NamedCallableQueryMemento; +import org.hibernate.query.NativeQuery; import org.hibernate.query.Query; import org.hibernate.query.criteria.HibernateCriteriaBuilder; import org.hibernate.query.hql.spi.HqlQueryImplementor; @@ -881,6 +882,21 @@ public abstract class AbstractSharedSessionContract implements SharedSessionCont throw getExceptionConverter().convert( new IllegalArgumentException( "No query defined for that name [" + queryName + "]" ) ); } + @Override @SuppressWarnings("unchecked") + public QueryImplementor createStatement(String statementString) { + return createQuery(statementString); + } + + @Override @SuppressWarnings("unchecked") + public QueryImplementor createNamedStatement(String name) { + return createNamedQuery(name); + } + + @Override @SuppressWarnings("unchecked") + public NativeQueryImplementor createNativeStatement(String sqlString) { + return createNativeQuery(sqlString); + } + @Override @SuppressWarnings("UnnecessaryLocalVariable") public ProcedureCall getNamedProcedureCall(String name) { diff --git a/hibernate-core/src/main/java/org/hibernate/query/QueryProducer.java b/hibernate-core/src/main/java/org/hibernate/query/QueryProducer.java index dcac365eca..f62cacc6db 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/QueryProducer.java +++ b/hibernate-core/src/main/java/org/hibernate/query/QueryProducer.java @@ -21,20 +21,24 @@ import jakarta.persistence.criteria.CriteriaUpdate; */ public interface QueryProducer { /** - * Create a {@link Query} instance for the given HQL/JPQL query string. + * Create a {@link Query} instance for the given HQL/JPQL query, or + * HQL/JPQL insert, update, or delete statement. * * @param queryString The HQL/JPQL query * * @return The Query instance for manipulation and execution * * @see jakarta.persistence.EntityManager#createQuery(String) - * @deprecated use {@link #createQuery(String, Class)} + * @deprecated use {@link #createQuery(String, Class)} or {@link #createStatement(String)} */ @Deprecated @SuppressWarnings("rawtypes") Query createQuery(String queryString); /** * Create a typed {@link Query} instance for the given HQL/JPQL query string. + *

+ * The returned {@code Query} may be executed by calling + * {@link Query#getResultList()} or {@link Query#getSingleResult()}. * * @param queryString The HQL/JPQL query * @param resultClass The type of the query result @@ -45,8 +49,20 @@ public interface QueryProducer { Query createQuery(String queryString, Class resultClass); /** - * The JPA-defined named query creation method. This form can represent an - * HQL/JPQL query or a native query. + * Create a typed {@link Query} instance for the given HQL/JPQL insert, + * update, or delete statement. + *

+ * The returned {@code Query} must be executed by calling + * {@link Query#executeUpdate()}. + * + * @param statementString The HQL/JPQL insert, update, or delete statement + * @return The Query instance for manipulation and execution + */ + Query createStatement(String statementString); + + /** + * Create a typed {@link Query} instance for the given named query. + * The named query might be defined in HQL or in native SQL. * * @param name the name of a pre-defined, named query * @@ -58,14 +74,14 @@ public interface QueryProducer { * * @see jakarta.persistence.EntityManager#createNamedQuery(String) * - * @deprecated use {@link #createNamedQuery(String, Class)} + * @deprecated use {@link #createNamedQuery(String, Class)} or {@link #createNamedStatement(String)} */ @Deprecated @SuppressWarnings("rawtypes") Query createNamedQuery(String name); /** - * The JPA-defined named, typed query creation method. This form can only - * represent an HQL/JPQL query (not a native query). + * Create a typed {@link Query} instance for the given named query. + * The named query might be defined in HQL or in native SQL. * * @param name the name of a query defined in metadata * @param resultClass the type of the query result @@ -82,7 +98,22 @@ public interface QueryProducer { Query createNamedQuery(String name, Class resultClass); /** - * Create a NativeQuery instance for the given native (SQL) query + * Create a typed {@link Query} instance for the given named insert, + * update, or delete statement. The named query might be defined in + * HQL or in native SQL. + * + * @param name the name of a pre-defined, named query + * + * @return The Query instance for manipulation and execution + * + * @throws IllegalArgumentException if a query has not been + * defined with the given name or if the query string is + * found to be invalid + */ + Query createNamedStatement(String name); + + /** + * Create a {@link NativeQuery} instance for the given native (SQL) query * * @param sqlString a native SQL query string * @@ -96,7 +127,7 @@ public interface QueryProducer { NativeQuery createNativeQuery(String sqlString); /** - * Create a NativeQuery instance for the given native (SQL) query using + * Create a {@link NativeQuery} instance for the given native (SQL) query using * implicit mapping to the specified Java type. *

* If the given class is an entity class, this method is equivalent to @@ -112,7 +143,7 @@ public interface QueryProducer { NativeQuery createNativeQuery(String sqlString, Class resultClass); /** - * Create a NativeQuery instance for the given native (SQL) query using + * Create a {@link NativeQuery} instance for the given native (SQL) query using * implicit mapping to the specified Java type. *

* If the given class is an entity class, this method is equivalent to @@ -129,7 +160,7 @@ public interface QueryProducer { NativeQuery createNativeQuery(String sqlString, Class resultClass, String tableAlias); /** - * Create a NativeQuery instance for the given native (SQL) query using + * Create a {@link NativeQuery} instance for the given native (SQL) query using * implicit mapping to the specified Java type. * * @param sqlString Native (SQL) query string @@ -146,7 +177,7 @@ public interface QueryProducer { NativeQuery createNativeQuery(String sqlString, String resultSetMappingName); /** - * Create a NativeQuery instance for the given native (SQL) query using + * Create a {@link NativeQuery} instance for the given native (SQL) query using * implicit mapping to the specified Java type. * * @param sqlString Native (SQL) query string @@ -160,21 +191,30 @@ public interface QueryProducer { NativeQuery createNativeQuery(String sqlString, String resultSetMappingName, Class resultClass); /** - * Create a Query for the given JPA {@link CriteriaQuery} + * Create a {@link NativeQuery} instance for the given native (SQL) statement + * + * @param sqlString a native SQL statement string + * + * @return The NativeQuery instance for manipulation and execution + */ + NativeQuery createNativeStatement(String sqlString); + + /** + * Create a {@link Query} for the given JPA {@link CriteriaQuery} * * @see jakarta.persistence.EntityManager#createQuery(CriteriaQuery) */ Query createQuery(CriteriaQuery criteriaQuery); /** - * Create a Query for the given JPA {@link CriteriaUpdate} + * Create a {@link Query} for the given JPA {@link CriteriaUpdate} * * @see jakarta.persistence.EntityManager#createQuery(CriteriaUpdate) */ Query createQuery(@SuppressWarnings("rawtypes") CriteriaUpdate updateQuery); /** - * Create a Query for the given JPA {@link CriteriaDelete} + * Create a {@link Query} for the given JPA {@link CriteriaDelete} * * @see jakarta.persistence.EntityManager#createQuery(CriteriaDelete) */ diff --git a/hibernate-core/src/main/java/org/hibernate/query/spi/QueryProducerImplementor.java b/hibernate-core/src/main/java/org/hibernate/query/spi/QueryProducerImplementor.java index 57b87dd9e0..a121ca9f09 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/spi/QueryProducerImplementor.java +++ b/hibernate-core/src/main/java/org/hibernate/query/spi/QueryProducerImplementor.java @@ -12,6 +12,8 @@ import jakarta.persistence.criteria.CriteriaUpdate; import org.hibernate.CacheMode; import org.hibernate.FlushMode; import org.hibernate.engine.spi.SessionFactoryImplementor; +import org.hibernate.query.NativeQuery; +import org.hibernate.query.Query; import org.hibernate.query.QueryProducer; import org.hibernate.query.sql.spi.NativeQueryImplementor; @@ -32,19 +34,19 @@ public interface QueryProducerImplementor extends QueryProducer { @Override @SuppressWarnings("rawtypes") QueryImplementor getNamedQuery(String queryName); - @Override @SuppressWarnings("rawtypes") + @Override @Deprecated @SuppressWarnings("rawtypes") QueryImplementor createQuery(String queryString); @Override QueryImplementor createQuery(String queryString, Class resultClass); - @Override @SuppressWarnings("rawtypes") + @Override @Deprecated @SuppressWarnings("rawtypes") QueryImplementor createNamedQuery(String name); @Override QueryImplementor createNamedQuery(String name, Class resultClass); - @Override @SuppressWarnings("rawtypes") + @Override @Deprecated @SuppressWarnings("rawtypes") NativeQueryImplementor createNativeQuery(String sqlString); @Override @@ -65,6 +67,15 @@ public interface QueryProducerImplementor extends QueryProducer { @Override @SuppressWarnings("rawtypes") NativeQueryImplementor getNamedNativeQuery(String name, String resultSetMapping); + @Override + QueryImplementor createStatement(String statementString); + + @Override + QueryImplementor createNamedStatement(String name); + + @Override + NativeQueryImplementor createNativeStatement(String sqlString); + @Override QueryImplementor createQuery(CriteriaQuery criteriaQuery);