diff --git a/hibernate-core/src/main/java/org/hibernate/engine/spi/RowSelection.java b/hibernate-core/src/main/java/org/hibernate/engine/spi/RowSelection.java index 099fd80e3e..a52b1c0ed1 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/spi/RowSelection.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/spi/RowSelection.java @@ -17,7 +17,7 @@ public final class RowSelection { private Integer timeout; private Integer fetchSize; - public void setFirstRow(Integer firstRow) { + public void setFirstRow(int firstRow) { this.firstRow = firstRow; } @@ -25,7 +25,7 @@ public final class RowSelection { return firstRow; } - public void setMaxRows(Integer maxRows) { + public void setMaxRows(int maxRows) { this.maxRows = maxRows; } @@ -33,7 +33,7 @@ public final class RowSelection { return maxRows; } - public void setTimeout(Integer timeout) { + public void setTimeout(int timeout) { this.timeout = timeout; } @@ -45,11 +45,15 @@ public final class RowSelection { return fetchSize; } - public void setFetchSize(Integer fetchSize) { + public void setFetchSize(int fetchSize) { this.fetchSize = fetchSize; } public boolean definesLimits() { return maxRows != null || (firstRow != null && firstRow <= 0); } + + public void unsetMaxRows() { + maxRows = null; + } } diff --git a/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java b/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java index 674d8c9e08..471ed98b7a 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java @@ -842,12 +842,24 @@ public final class SessionFactoryImpl implements SessionFactoryImplementor { .setCacheable( query.isCacheable() ) .setCacheRegion( query.getCacheRegion() ) .setCacheMode( query.getCacheMode() ) - .setTimeout( query.getTimeout() ) - .setFetchSize( query.getFetchSize() ) - .setFirstResult( query.getFirstResult() ) - .setMaxResults( query.getMaxResults() ) .setReadOnly( query.isReadOnly() ) .setFlushMode( query.getHibernateFlushMode() ); + + if ( query.getQueryOptions().getFirstRow() != null ) { + builder.setFirstResult( query.getQueryOptions().getFirstRow() ); + } + + if ( query.getQueryOptions().getMaxRows() != null ) { + builder.setMaxResults( query.getQueryOptions().getMaxRows() ); + } + + if ( query.getQueryOptions().getTimeout() != null ) { + builder.setTimeout( query.getQueryOptions().getTimeout() ); + } + + if ( query.getQueryOptions().getFetchSize() != null ) { + builder.setFetchSize( query.getQueryOptions().getFetchSize() ); + } } @Override diff --git a/hibernate-core/src/main/java/org/hibernate/jpa/boot/internal/EntityManagerFactoryBuilderImpl.java b/hibernate-core/src/main/java/org/hibernate/jpa/boot/internal/EntityManagerFactoryBuilderImpl.java index 7ffda8dc17..645105b157 100644 --- a/hibernate-core/src/main/java/org/hibernate/jpa/boot/internal/EntityManagerFactoryBuilderImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/jpa/boot/internal/EntityManagerFactoryBuilderImpl.java @@ -25,7 +25,6 @@ import javax.sql.DataSource; import javassist.CtClass; import javassist.CtField; -import org.hibernate.Interceptor; import org.hibernate.SessionFactory; import org.hibernate.SessionFactoryObserver; import org.hibernate.boot.CacheRegionDefinition; @@ -81,11 +80,24 @@ import org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator; import org.jboss.jandex.Index; +import static org.hibernate.cfg.AvailableSettings.DATASOURCE; +import static org.hibernate.cfg.AvailableSettings.DRIVER; import static org.hibernate.cfg.AvailableSettings.JACC_CONTEXT_ID; import static org.hibernate.cfg.AvailableSettings.JACC_PREFIX; +import static org.hibernate.cfg.AvailableSettings.JPA_JDBC_DRIVER; +import static org.hibernate.cfg.AvailableSettings.JPA_JDBC_PASSWORD; +import static org.hibernate.cfg.AvailableSettings.JPA_JDBC_URL; +import static org.hibernate.cfg.AvailableSettings.JPA_JDBC_USER; +import static org.hibernate.cfg.AvailableSettings.JPA_JTA_DATASOURCE; +import static org.hibernate.cfg.AvailableSettings.JPA_NON_JTA_DATASOURCE; import static org.hibernate.cfg.AvailableSettings.JPA_SHARED_CACHE_MODE; +import static org.hibernate.cfg.AvailableSettings.JPA_TRANSACTION_TYPE; import static org.hibernate.cfg.AvailableSettings.JPA_VALIDATION_MODE; +import static org.hibernate.cfg.AvailableSettings.PASS; import static org.hibernate.cfg.AvailableSettings.SESSION_FACTORY_NAME; +import static org.hibernate.cfg.AvailableSettings.TRANSACTION_COORDINATOR_STRATEGY; +import static org.hibernate.cfg.AvailableSettings.URL; +import static org.hibernate.cfg.AvailableSettings.USER; import static org.hibernate.internal.HEMLogging.messageLogger; import static org.hibernate.jpa.AvailableSettings.CFG_FILE; import static org.hibernate.jpa.AvailableSettings.CLASS_CACHE_PREFIX; @@ -314,9 +326,8 @@ public class EntityManagerFactoryBuilderImpl implements EntityManagerFactoryBuil final StrategyRegistrationProviderList strategyRegistrationProviderList = (StrategyRegistrationProviderList) integrationSettings.get( STRATEGY_REGISTRATION_PROVIDERS ); if ( strategyRegistrationProviderList != null ) { - for ( StrategyRegistrationProvider strategyRegistrationProvider : strategyRegistrationProviderList - .getStrategyRegistrationProviders() ) { - bsrBuilder.withStrategySelectors( strategyRegistrationProvider ); + for ( StrategyRegistrationProvider strategyRegistrationProvider : strategyRegistrationProviderList.getStrategyRegistrationProviders() ) { + bsrBuilder.applyStrategySelectors( strategyRegistrationProvider ); } } @@ -574,45 +585,45 @@ public class EntityManagerFactoryBuilderImpl implements EntityManagerFactoryBuil private void applyJdbcConnectionProperties(StandardServiceRegistryBuilder ssrBuilder) { if ( dataSource != null ) { - ssrBuilder.applySetting( org.hibernate.cfg.AvailableSettings.DATASOURCE, dataSource ); + ssrBuilder.applySetting( DATASOURCE, dataSource ); } else if ( persistenceUnit.getJtaDataSource() != null ) { - if ( ! ssrBuilder.getSettings().containsKey( org.hibernate.cfg.AvailableSettings.DATASOURCE ) ) { - ssrBuilder.applySetting( org.hibernate.cfg.AvailableSettings.DATASOURCE, persistenceUnit.getJtaDataSource() ); + if ( ! ssrBuilder.getSettings().containsKey( DATASOURCE ) ) { + ssrBuilder.applySetting( DATASOURCE, persistenceUnit.getJtaDataSource() ); // HHH-8121 : make the PU-defined value available to EMF.getProperties() - configurationValues.put( AvailableSettings.JTA_DATASOURCE, persistenceUnit.getJtaDataSource() ); + configurationValues.put( JPA_JTA_DATASOURCE, persistenceUnit.getJtaDataSource() ); } } else if ( persistenceUnit.getNonJtaDataSource() != null ) { - if ( ! ssrBuilder.getSettings().containsKey( org.hibernate.cfg.AvailableSettings.DATASOURCE ) ) { - ssrBuilder.applySetting( org.hibernate.cfg.AvailableSettings.DATASOURCE, persistenceUnit.getNonJtaDataSource() ); + if ( ! ssrBuilder.getSettings().containsKey( DATASOURCE ) ) { + ssrBuilder.applySetting( DATASOURCE, persistenceUnit.getNonJtaDataSource() ); // HHH-8121 : make the PU-defined value available to EMF.getProperties() - configurationValues.put( AvailableSettings.NON_JTA_DATASOURCE, persistenceUnit.getNonJtaDataSource() ); + configurationValues.put( JPA_NON_JTA_DATASOURCE, persistenceUnit.getNonJtaDataSource() ); } } else { - final String driver = (String) configurationValues.get( AvailableSettings.JDBC_DRIVER ); + final String driver = (String) configurationValues.get( JPA_JDBC_DRIVER ); if ( StringHelper.isNotEmpty( driver ) ) { - ssrBuilder.applySetting( org.hibernate.cfg.AvailableSettings.DRIVER, driver ); + ssrBuilder.applySetting( DRIVER, driver ); } - final String url = (String) configurationValues.get( AvailableSettings.JDBC_URL ); + final String url = (String) configurationValues.get( JPA_JDBC_URL ); if ( StringHelper.isNotEmpty( url ) ) { - ssrBuilder.applySetting( org.hibernate.cfg.AvailableSettings.URL, url ); + ssrBuilder.applySetting( URL, url ); } - final String user = (String) configurationValues.get( AvailableSettings.JDBC_USER ); + final String user = (String) configurationValues.get( JPA_JDBC_USER ); if ( StringHelper.isNotEmpty( user ) ) { - ssrBuilder.applySetting( org.hibernate.cfg.AvailableSettings.USER, user ); + ssrBuilder.applySetting( USER, user ); } - final String pass = (String) configurationValues.get( AvailableSettings.JDBC_PASSWORD ); + final String pass = (String) configurationValues.get( JPA_JDBC_PASSWORD ); if ( StringHelper.isNotEmpty( pass ) ) { - ssrBuilder.applySetting( org.hibernate.cfg.AvailableSettings.PASS, pass ); + ssrBuilder.applySetting( PASS, pass ); } } } private void applyTransactionProperties(StandardServiceRegistryBuilder ssrBuilder, SettingsImpl settings) { PersistenceUnitTransactionType txnType = PersistenceUnitTransactionTypeHelper.interpretTransactionType( - configurationValues.get( AvailableSettings.TRANSACTION_TYPE ) + configurationValues.get( JPA_TRANSACTION_TYPE ) ); if ( txnType == null ) { txnType = persistenceUnit.getTransactionType(); @@ -622,31 +633,20 @@ public class EntityManagerFactoryBuilderImpl implements EntityManagerFactoryBuil txnType = PersistenceUnitTransactionType.RESOURCE_LOCAL; } settings.setTransactionType( txnType ); - boolean hasTxStrategy = configurationValues.containsKey( Environment.TRANSACTION_COORDINATOR_STRATEGY ); + boolean hasTxStrategy = configurationValues.containsKey( TRANSACTION_COORDINATOR_STRATEGY ); if ( hasTxStrategy ) { - LOG.overridingTransactionStrategyDangerous( Environment.TRANSACTION_COORDINATOR_STRATEGY ); + LOG.overridingTransactionStrategyDangerous( TRANSACTION_COORDINATOR_STRATEGY ); } else { if ( txnType == PersistenceUnitTransactionType.JTA ) { - ssrBuilder.applySetting( Environment.TRANSACTION_COORDINATOR_STRATEGY, JtaTransactionCoordinatorBuilderImpl.class ); + ssrBuilder.applySetting( TRANSACTION_COORDINATOR_STRATEGY, JtaTransactionCoordinatorBuilderImpl.class ); } else if ( txnType == PersistenceUnitTransactionType.RESOURCE_LOCAL ) { - ssrBuilder.applySetting( Environment.TRANSACTION_COORDINATOR_STRATEGY, JdbcResourceLocalTransactionCoordinatorBuilderImpl.class ); + ssrBuilder.applySetting( TRANSACTION_COORDINATOR_STRATEGY, JdbcResourceLocalTransactionCoordinatorBuilderImpl.class ); } } } - @SuppressWarnings("unchecked") - private Class loadSessionInterceptorClass(Object value, StrategySelector strategySelector) { - if ( value == null ) { - return null; - } - - return Class.class.isInstance( value ) - ? (Class) value - : strategySelector.selectStrategyImplementor( Interceptor.class, value.toString() ); - } - private void configure(StandardServiceRegistry ssr, MergedSettings mergedSettings) { final StrategySelector strategySelector = ssr.getService( StrategySelector.class ); @@ -722,7 +722,7 @@ public class EntityManagerFactoryBuilderImpl implements EntityManagerFactoryBuil for ( Class cls : loadedAnnotatedClasses ) { if ( AttributeConverter.class.isAssignableFrom( cls ) ) { if ( attributeConverterDefinitions == null ) { - attributeConverterDefinitions = new ArrayList(); + attributeConverterDefinitions = new ArrayList<>(); } attributeConverterDefinitions.add( AttributeConverterDefinition.from( (Class) cls ) ); } @@ -902,7 +902,7 @@ public class EntityManagerFactoryBuilderImpl implements EntityManagerFactoryBuil } - public static class ServiceRegistryCloser implements SessionFactoryObserver { + private static class ServiceRegistryCloser implements SessionFactoryObserver { /** * Singleton access */ @@ -937,22 +937,22 @@ public class EntityManagerFactoryBuilderImpl implements EntityManagerFactoryBuil return "[PersistenceUnit: " + persistenceUnit.getName() + "] "; } - public static class MergedSettings { + private static class MergedSettings { private final Map configurationValues = new ConcurrentHashMap( 16, 0.75f, 1 ); private Map jaccPermissionsByContextId; private List cacheRegionDefinitions; - public MergedSettings() { + private MergedSettings() { } public Map getConfigurationValues() { return configurationValues; } - public JaccPermissionDeclarations getJaccPermissions(String jaccContextId) { + private JaccPermissionDeclarations getJaccPermissions(String jaccContextId) { if ( jaccPermissionsByContextId == null ) { - jaccPermissionsByContextId = new HashMap(); + jaccPermissionsByContextId = new HashMap<>(); } JaccPermissionDeclarations jaccPermissions = jaccPermissionsByContextId.get( jaccContextId ); @@ -963,9 +963,9 @@ public class EntityManagerFactoryBuilderImpl implements EntityManagerFactoryBuil return jaccPermissions; } - public void addCacheRegionDefinition(CacheRegionDefinition cacheRegionDefinition) { + private void addCacheRegionDefinition(CacheRegionDefinition cacheRegionDefinition) { if ( this.cacheRegionDefinitions == null ) { - this.cacheRegionDefinitions = new ArrayList(); + this.cacheRegionDefinitions = new ArrayList<>(); } this.cacheRegionDefinitions.add( cacheRegionDefinition ); } diff --git a/hibernate-core/src/main/java/org/hibernate/procedure/internal/ProcedureCallImpl.java b/hibernate-core/src/main/java/org/hibernate/procedure/internal/ProcedureCallImpl.java index 0c380bb97b..9900860a43 100644 --- a/hibernate-core/src/main/java/org/hibernate/procedure/internal/ProcedureCallImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/procedure/internal/ProcedureCallImpl.java @@ -49,10 +49,8 @@ import org.hibernate.procedure.ProcedureOutputs; import org.hibernate.procedure.spi.ParameterRegistrationImplementor; import org.hibernate.procedure.spi.ParameterStrategy; import org.hibernate.procedure.spi.ProcedureCallImplementor; -import org.hibernate.query.ParameterMetadata; import org.hibernate.query.QueryParameter; import org.hibernate.query.internal.AbstractProducedQuery; -import org.hibernate.query.spi.QueryParameterBindings; import org.hibernate.result.NoMoreReturnsException; import org.hibernate.result.Output; import org.hibernate.result.ResultSetOutput; @@ -785,18 +783,12 @@ public class ProcedureCallImpl @Override @SuppressWarnings("unchecked") public T unwrap(Class cls) { - if ( ProcedureCall.class.isAssignableFrom( cls ) ) { + if ( cls.isInstance( this ) ) { return (T) this; } - else if ( ProcedureOutputs.class.isAssignableFrom( cls ) ) { + else if ( cls.isInstance( outputs ) ) { return (T) outputs(); } - else if ( ParameterMetadata.class.isAssignableFrom( cls ) ) { - return (T) getParameterMetadata(); - } - else if ( QueryParameterBindings.class.isAssignableFrom( cls ) ) { - return (T) getQueryParameterBindings(); - } return super.unwrap( cls ); } 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 eb55102cea..3950dde4d9 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/Query.java +++ b/hibernate-core/src/main/java/org/hibernate/query/Query.java @@ -19,6 +19,7 @@ import org.hibernate.FlushMode; import org.hibernate.Incubating; import org.hibernate.LockMode; import org.hibernate.LockOptions; +import org.hibernate.engine.spi.RowSelection; /** * Represents an HQL/JPQL query or a compiled Criteria query. Also acts as the Hibernate @@ -40,6 +41,15 @@ public interface Query extends TypedQuery, org.hibernate.Query, BasicQu */ QueryProducer getProducer(); + /** + * "QueryOptions" is a better name, I think, than "RowSelection" -> 6.0 + * + * @return Return the encapsulation of this query's options, which includes access to + * firstRow, maxRows, timeout and fetchSize. Important because this gives access to + * those values in their Integer form rather than the primitive form (int) required by JPA. + */ + RowSelection getQueryOptions(); + // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // covariant overrides diff --git a/hibernate-core/src/main/java/org/hibernate/query/internal/AbstractProducedQuery.java b/hibernate-core/src/main/java/org/hibernate/query/internal/AbstractProducedQuery.java index a53c5e4459..93b12616da 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/internal/AbstractProducedQuery.java +++ b/hibernate-core/src/main/java/org/hibernate/query/internal/AbstractProducedQuery.java @@ -90,21 +90,18 @@ public abstract class AbstractProducedQuery implements QueryImplementor { private FlushMode flushMode; private CacheMode cacheMode; - private Integer timeout; private boolean cacheable; private String cacheRegion; private Boolean readOnly; private LockOptions lockOptions = new LockOptions(); - private Integer fetchSize; - private String comment; private final List dbHints = new ArrayList<>(); private Map hints; private ResultTransformer resultTransformer; - private RowSelection selection = new RowSelection(); + private RowSelection queryOptions = new RowSelection(); private HQLQueryPlan entityGraphHintedQueryPlan; private Object optionalObject; @@ -194,25 +191,25 @@ public abstract class AbstractProducedQuery implements QueryImplementor { @Override public Integer getTimeout() { - return timeout; + return queryOptions.getTimeout(); } @Override @SuppressWarnings("unchecked") public QueryImplementor setTimeout(int timeout) { - this.timeout = timeout; + queryOptions.setTimeout( timeout ); return this; } @Override public Integer getFetchSize() { - return fetchSize; + return queryOptions.getFetchSize(); } @Override @SuppressWarnings("unchecked") public QueryImplementor setFetchSize(int fetchSize) { - this.fetchSize = fetchSize; + queryOptions.setFetchSize( fetchSize ); return this; } @@ -310,9 +307,21 @@ public abstract class AbstractProducedQuery implements QueryImplementor { else if ( value instanceof Collection ) { setParameterList( (QueryParameter) parameter, (Collection) value ); } - else { + else if ( parameter instanceof QueryParameter ) { queryParameterBindings.getBinding( (QueryParameter) parameter ).setBindValue( value ); } + else if ( parameter.getName() != null ) { + queryParameterBindings.getBinding( parameter.getName() ).setBindValue( value ); + } + else if ( parameter.getPosition() != null ) { + queryParameterBindings.getBinding( parameter.getPosition() ).setBindValue( value ); + } + else { + throw getExceptionConverter().convert( + new IllegalArgumentException( "Could not resolve parameter instance [" + parameter + "] as query parameter" ) + + ); + } return this; } @@ -603,9 +612,16 @@ public abstract class AbstractProducedQuery implements QueryImplementor { return this; } + @Override + public RowSelection getQueryOptions() { + return queryOptions; + } + @Override public int getMaxResults() { - return selection.getMaxRows(); + // to be JPA compliant this method returns an int - specifically the "magic number" Integer.MAX_VALUE defined by the spec. + // For access to the Integer (for checking), use #getQueryOptions#getMaxRows instead + return queryOptions.getMaxRows() == null ? Integer.MAX_VALUE : queryOptions.getMaxRows(); } @Override @@ -613,23 +629,25 @@ public abstract class AbstractProducedQuery implements QueryImplementor { public QueryImplementor setMaxResults(int maxResult) { if ( maxResult <= 0 ) { // treat zero and negatives specially as meaning no limit... - selection.setMaxRows( null ); + queryOptions.unsetMaxRows(); } else { - selection.setMaxRows( maxResult ); + queryOptions.setMaxRows( maxResult ); } return this; } @Override public int getFirstResult() { - return selection.getFirstRow(); + // to be JPA compliant this method returns an int - specifically the "magic number" 0 (ZERO) defined by the spec. + // For access to the Integer (for checking), use #getQueryOptions#getFirstRow instead + return queryOptions.getFirstRow() == null ? 0 : queryOptions.getFirstRow(); } @Override @SuppressWarnings("unchecked") public QueryImplementor setFirstResult(int startPosition) { - selection.setFirstRow( startPosition ); + queryOptions.setFirstRow( startPosition ); return this; } @@ -964,7 +982,8 @@ public abstract class AbstractProducedQuery implements QueryImplementor { return (T) this; } - throw new IllegalArgumentException( "Could not unwrap this [" + toString() + "] as requested Java type [" + cls.getName() + "]" ); + throw new HibernateException( "Could not unwrap this [" + toString() + "] as requested Java type [" + cls.getName() + "]" ); +// throw new IllegalArgumentException( "Could not unwrap this [" + toString() + "] as requested Java type [" + cls.getName() + "]" ); } public QueryParameters getQueryParameters() { @@ -973,7 +992,7 @@ public abstract class AbstractProducedQuery implements QueryImplementor { getPositionalParameterValues(), getNamedParameterMap(), getLockOptions(), - selection, + queryOptions, true, isReadOnly(), cacheable, diff --git a/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/query/AddNamedQueryTest.java b/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/query/AddNamedQueryTest.java index d42911f82a..371d5ef2de 100644 --- a/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/query/AddNamedQueryTest.java +++ b/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/query/AddNamedQueryTest.java @@ -16,7 +16,6 @@ import org.hibernate.FlushMode; import org.hibernate.LockMode; import org.hibernate.engine.spi.NamedQueryDefinition; import org.hibernate.engine.spi.SessionFactoryImplementor; -import org.hibernate.jpa.HibernateQuery; import org.hibernate.jpa.QueryHints; import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; import org.hibernate.jpa.test.Distributor; @@ -116,24 +115,29 @@ public class AddNamedQueryTest extends BaseEntityManagerFunctionalTestCase { em.getTransaction().begin(); Query query = em.createNamedQuery( name ); - org.hibernate.Query hibernateQuery = ( (HibernateQuery) query ).getHibernateQuery(); + org.hibernate.query.Query hibernateQuery = (org.hibernate.query.Query) query; // assert the state of the query config settings based on the initial named query - assertNull( hibernateQuery.getFirstResult() ); - assertNull( hibernateQuery.getMaxResults() ); - assertEquals( FlushMode.AUTO, hibernateQuery.getFlushMode() ); + // + // NOTE: here we check "query options" via the Hibernate contract (allowing nullness checking); see below for access via the JPA contract + assertNull( hibernateQuery.getQueryOptions().getFirstRow() ); + assertNull( hibernateQuery.getQueryOptions().getMaxRows() ); + assertEquals( FlushMode.AUTO, hibernateQuery.getHibernateFlushMode() ); assertEquals( CacheMode.IGNORE, hibernateQuery.getCacheMode() ); assertEquals( LockMode.PESSIMISTIC_WRITE, hibernateQuery.getLockOptions().getLockMode() ); - assertEquals( (Integer) 3, hibernateQuery.getTimeout() ); // jpa timeout is in milliseconds, whereas Hibernate's is in seconds + // jpa timeout is in milliseconds, whereas Hibernate's is in seconds + assertEquals( (Integer) 3, hibernateQuery.getTimeout() ); query.setHint( QueryHints.HINT_TIMEOUT, 10 ); em.getEntityManagerFactory().addNamedQuery( name, query ); query = em.createNamedQuery( name ); - hibernateQuery = ( (HibernateQuery) query ).getHibernateQuery(); + hibernateQuery = (org.hibernate.query.Query) query; // assert the state of the query config settings based on the initial named query - assertNull( hibernateQuery.getFirstResult() ); - assertNull( hibernateQuery.getMaxResults() ); - assertEquals( FlushMode.AUTO, hibernateQuery.getFlushMode() ); + // + // NOTE: here we check "query options" via the JPA contract + assertEquals( 0, hibernateQuery.getFirstResult() ); + assertEquals( Integer.MAX_VALUE, hibernateQuery.getMaxResults() ); + assertEquals( FlushModeType.AUTO, hibernateQuery.getFlushMode() ); assertEquals( CacheMode.IGNORE, hibernateQuery.getCacheMode() ); assertEquals( LockMode.PESSIMISTIC_WRITE, hibernateQuery.getLockOptions().getLockMode() ); assertEquals( (Integer) 10, hibernateQuery.getTimeout() ); @@ -142,11 +146,11 @@ public class AddNamedQueryTest extends BaseEntityManagerFunctionalTestCase { em.getEntityManagerFactory().addNamedQuery( name, query ); query = em.createNamedQuery( name ); - hibernateQuery = ( (HibernateQuery) query ).getHibernateQuery(); + hibernateQuery = (org.hibernate.query.Query) query; // assert the state of the query config settings based on the initial named query - assertNull( hibernateQuery.getFirstResult() ); - assertNull( hibernateQuery.getMaxResults() ); - assertEquals( FlushMode.AUTO, hibernateQuery.getFlushMode() ); + assertEquals( 0, hibernateQuery.getFirstResult() ); + assertEquals( Integer.MAX_VALUE, hibernateQuery.getMaxResults() ); + assertEquals( FlushModeType.AUTO, hibernateQuery.getFlushMode() ); assertEquals( CacheMode.IGNORE, hibernateQuery.getCacheMode() ); assertEquals( LockMode.PESSIMISTIC_WRITE, hibernateQuery.getLockOptions().getLockMode() ); assertEquals( (Integer) 10, hibernateQuery.getTimeout() ); @@ -155,11 +159,11 @@ public class AddNamedQueryTest extends BaseEntityManagerFunctionalTestCase { em.getEntityManagerFactory().addNamedQuery( name, query ); query = em.createNamedQuery( name ); - hibernateQuery = ( (HibernateQuery) query ).getHibernateQuery(); + hibernateQuery = (org.hibernate.query.Query) query; // assert the state of the query config settings based on the initial named query assertEquals( 51, hibernateQuery.getFirstResult() ); - assertNull( hibernateQuery.getMaxResults() ); - assertEquals( FlushMode.AUTO, hibernateQuery.getFlushMode() ); + assertEquals( Integer.MAX_VALUE, hibernateQuery.getMaxResults() ); + assertEquals( FlushModeType.AUTO, hibernateQuery.getFlushMode() ); assertEquals( CacheMode.IGNORE, hibernateQuery.getCacheMode() ); assertEquals( LockMode.PESSIMISTIC_WRITE, hibernateQuery.getLockOptions().getLockMode() ); assertEquals( (Integer) 10, hibernateQuery.getTimeout() ); diff --git a/migration-guide.adoc b/migration-guide.adoc index e816569cd4..54ee496744 100644 --- a/migration-guide.adoc +++ b/migration-guide.adoc @@ -26,6 +26,11 @@ The hibernate-entitymanager module has also been merged into hibernate-core. * `org.hibernate.Session` now extends `javax.persistence.EntityManager` - temporarily it technically extends `org.hibernate.jpa.HibernateEntityManager` (which in turn extends `javax.persistence.EntityManager`) for backwards compatibility. `HibernateEntityManager` is deprecated. +* `org.hibernate.Query` (deprecated in favor of new `org.hibernate.query.Query`) now extends the JPA contracts + `javax.persistence.Query` and `javax.persistence.TypedQuery`. `ProcedureCall` and `StoredProcedureQuery` as well. +* `org.hibernate.HibernateException` now extends `javax.persistence.PersistenceExceptions`. Hibernate methods + that "override" methods from their JPA counterparts now will also throw various JDK defined RuntimeExceptions + (such as `IllegalArgumentException`, `IllegalStateException`, etc) as required by the JPA contract. * Persister/type access is now exposed through `org.hibernate.Metamodel`, which extends `javax.persistence.metamodel.Metamodel`. MetamodelImpl now manages all aspects of type system (see below). * Cache management has also been consolidated. `org.hibernate.Cache` now extends `javax.persistence.Cache`. CacheImpl