6 - Remove setParameter(... Type type) methods in favour of setParameter( AllowableParameterType type)

This commit is contained in:
Andrea Boriero 2019-07-17 17:33:57 +01:00
parent a5414d2324
commit a15774b7e0
9 changed files with 184 additions and 147 deletions

View File

@ -14,6 +14,7 @@ import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.internal.CoreLogging;
import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.loader.collection.CollectionInitializer;
import org.hibernate.metamodel.model.domain.AllowableParameterType;
import org.hibernate.query.sql.spi.NativeQueryImplementor;
/**
@ -42,11 +43,11 @@ public final class NamedQueryCollectionInitializer implements CollectionInitiali
nativeQuery.setParameter(
nativeQuery.getParameterMetadata().getNamedParameterNames().iterator().next(),
key,
persister.getKeyType()
(AllowableParameterType) persister.getKeyType()
);
}
else {
nativeQuery.setParameter( 1, key, persister.getKeyType() );
nativeQuery.setParameter( 1, key, (AllowableParameterType) persister.getKeyType() );
}
nativeQuery.setCollectionKey( key ).setHibernateFlushMode( FlushMode.MANUAL ).list();

View File

@ -14,6 +14,7 @@ import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.internal.CoreLogging;
import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.loader.entity.UniqueEntityLoader;
import org.hibernate.metamodel.model.domain.AllowableParameterType;
import org.hibernate.query.internal.AbstractProducedQuery;
/**
@ -61,10 +62,12 @@ public final class NamedQueryLoader implements UniqueEntityLoader {
final AbstractProducedQuery query = (AbstractProducedQuery) session.getNamedQuery( queryName );
if ( query.getParameterMetadata().hasNamedParameters() ) {
query.setParameter( query.getParameterMetadata().getNamedParameterNames().iterator().next(), id, persister.getIdentifierType() );
query.setParameter( query.getParameterMetadata().getNamedParameterNames().iterator().next(), id,
(AllowableParameterType) persister.getIdentifierType()
);
}
else {
query.setParameter( position, id, persister.getIdentifierType() );
query.setParameter( position, id, (AllowableParameterType) persister.getIdentifierType() );
}
query.setOptionalId( id );

View File

@ -851,20 +851,20 @@ public class ProcedureCallImpl<R>
return (ProcedureCallImplementor<R>) super.setParameter( parameter, value, type );
}
@Override
public <P> ProcedureCallImplementor<R> setParameter(QueryParameter<P> parameter, P value, Type type) {
return (ProcedureCallImplementor<R>) super.setParameter( parameter, value, type );
}
// @Override
// public <P> ProcedureCallImplementor<R> setParameter(QueryParameter<P> parameter, P value, Type type) {
// return (ProcedureCallImplementor<R>) super.setParameter( parameter, value, type );
// }
@Override
public ProcedureCallImplementor<R> setParameter(String name, Object value, AllowableParameterType type) {
return (ProcedureCallImplementor<R>) super.setParameter( name, value, type );
}
@Override
public ProcedureCallImplementor<R> setParameter(String name, Object value, Type type) {
return (ProcedureCallImplementor<R>) super.setParameter( name, value, type );
}
// @Override
// public ProcedureCallImplementor<R> setParameter(String name, Object value, Type type) {
// return (ProcedureCallImplementor<R>) super.setParameter( name, value, type );
// }
@Override
public ProcedureCallImplementor<R> setParameter(int position, Object value, AllowableParameterType type) {
@ -872,10 +872,10 @@ public class ProcedureCallImpl<R>
return (ProcedureCallImplementor<R>) super.setParameter( position, value, type );
}
@Override
public ProcedureCallImplementor<R> setParameter(int position, Object value, Type type) {
return (ProcedureCallImplementor<R>) super.setParameter( position, value, type );
}
// @Override
// public ProcedureCallImplementor<R> setParameter(int position, Object value, Type type) {
// return (ProcedureCallImplementor<R>) super.setParameter( position, value, type );
// }
@Override
public <P> ProcedureCallImplementor<R> setParameter(QueryParameter<P> parameter, P value, TemporalType temporalPrecision) {

View File

@ -480,14 +480,14 @@ public interface NativeQuery<T> extends Query<T>, SynchronizeableQuery {
<P> NativeQuery<T> setParameter(Parameter<P> param, P value);
@Override
NativeQuery<T> setParameter(String name, Object val, Type type);
@Override
NativeQuery<T> setParameter(int position, Object val, Type type);
@Override
<P> NativeQuery<T> setParameter(QueryParameter<P> parameter, P val, Type type);
// @Override
// NativeQuery<T> setParameter(String name, Object val, Type type);
//
// @Override
// NativeQuery<T> setParameter(int position, Object val, Type type);
//
// @Override
// <P> NativeQuery<T> setParameter(QueryParameter<P> parameter, P val, Type type);
@Override
@ -526,16 +526,16 @@ public interface NativeQuery<T> extends Query<T>, SynchronizeableQuery {
@Override
NativeQuery<T> setParameterList(String name, Collection values);
@Override
default NativeQuery<T> setParameterList(String name, Collection values, Type type) {
return setParameterList( name, values, (AllowableParameterType) type );
}
// @Override
// default NativeQuery<T> setParameterList(String name, Collection values, Type type) {
// return setParameterList( name, values, (AllowableParameterType) type );
// }
@Override
NativeQuery<T> setParameterList(String name, Collection values, AllowableParameterType type);
@Override
NativeQuery<T> setParameterList(String name, Object[] values, Type type);
// @Override
// NativeQuery<T> setParameterList(String name, Object[] values, Type type);
@Override
NativeQuery<T> setParameterList(String name, Object[] values, AllowableParameterType type);
@ -561,14 +561,14 @@ public interface NativeQuery<T> extends Query<T>, SynchronizeableQuery {
@Override
NativeQuery<T> setParameterList(int position, Collection values, Class type);
@Override
NativeQuery<T> setParameterList(int position, Collection values, Type type);
// @Override
// NativeQuery<T> setParameterList(int position, Collection values, Type type);
@Override
NativeQuery<T> setParameterList(int position, Collection values, AllowableParameterType type);
@Override
NativeQuery<T> setParameterList(int position, Object[] values, Type type);
// @Override
// NativeQuery<T> setParameterList(int position, Object[] values, Type type);
@Override
NativeQuery<T> setParameterList(int position, Object[] values, AllowableParameterType type);

View File

@ -564,10 +564,10 @@ public interface Query<R> extends TypedQuery<R>, CommonQueryContract {
*
* @deprecated Use {@link #setParameterList(String, Collection, AllowableParameterType)}
*/
@Deprecated
default Query<R> setParameterList(String name, Collection values, Type type){
return setParameter( name, values, (AllowableParameterType) type );
}
// @Deprecated
// default Query<R> setParameterList(String name, Collection values, Type type){
// return setParameter( name, values, (AllowableParameterType) type );
// }
/**
* Bind multiple values to a named query parameter. This is useful for binding
@ -581,10 +581,10 @@ public interface Query<R> extends TypedQuery<R>, CommonQueryContract {
*
* @deprecated Use {@link #setParameterList(String, Collection, AllowableParameterType)}
*/
@Deprecated
default Query<R> setParameterList(int position, Collection values, Type type){
return setParameter( position, values, (AllowableParameterType) type );
}
// @Deprecated
// default Query<R> setParameterList(int position, Collection values, Type type){
// return setParameter( position, values, (AllowableParameterType) type );
// }
/**
* Bind multiple values to a named query parameter. This is useful for binding
@ -623,9 +623,9 @@ public interface Query<R> extends TypedQuery<R>, CommonQueryContract {
* @deprecated Use {@link #setParameterList(String, Object[], AllowableParameterType)}
*/
@Deprecated
default Query<R> setParameterList(String name, Object[] values, Type type){
return setParameter( name, values, (AllowableParameterType)type );
}
// default Query<R> setParameterList(String name, Object[] values, Type type){
// return setParameter( name, values, (AllowableParameterType)type );
// }
/**
* Bind multiple values to a named query parameter. This is useful for binding
@ -639,10 +639,10 @@ public interface Query<R> extends TypedQuery<R>, CommonQueryContract {
*
* @deprecated Use {@link #setParameterList(String, Object[], AllowableParameterType)}
*/
@Deprecated
default Query<R> setParameterList(int position, Object[] values, Type type){
return setParameter( position, values, (AllowableParameterType)type );
}
// @Deprecated
// default Query<R> setParameterList(int position, Object[] values, Type type){
// return setParameter( position, values, (AllowableParameterType)type );
// }
/**
* Bind multiple values to a named query parameter. This is useful for binding
@ -733,10 +733,10 @@ public interface Query<R> extends TypedQuery<R>, CommonQueryContract {
*
* @deprecated Use {@link #setParameter(String, Object, AllowableParameterType)}
*/
@Deprecated
default Query<R> setParameter(String name, Object val, Type type){
return setParameter( name, val, (AllowableParameterType) type );
}
// @Deprecated
// default Query<R> setParameter(String name, Object val, Type type){
// return setParameter( name, val, (AllowableParameterType) type );
// }
/**
* Bind a value to a JDBC-style query parameter.
@ -750,10 +750,10 @@ public interface Query<R> extends TypedQuery<R>, CommonQueryContract {
*
* @deprecated Use {@link #setParameter(int, Object, AllowableParameterType)}
*/
@Deprecated
default Query<R> setParameter(int position, Object val, Type type) {
return setParameter( position, val, (AllowableParameterType) type );
}
// @Deprecated
// default Query<R> setParameter(int position, Object val, Type type) {
// return setParameter( position, val, (AllowableParameterType) type );
// }
/**
* Bind a query parameter using the supplied Type
@ -766,10 +766,10 @@ public interface Query<R> extends TypedQuery<R>, CommonQueryContract {
*
* @deprecated Use {@link #setParameter(QueryParameter, Object, AllowableParameterType)}
*/
@Deprecated
default <P> Query<R> setParameter(QueryParameter<P> parameter, P val, Type type){
return setParameter( parameter, val, (AllowableParameterType) type );
}
// @Deprecated
// default <P> Query<R> setParameter(QueryParameter<P> parameter, P val, Type type){
// return setParameter( parameter, val, (AllowableParameterType) type );
// }
/**
* @deprecated (since 5.2) Use {@link #setTupleTransformer} or {@link #setResultListTransformer}

View File

@ -832,24 +832,24 @@ public class NativeQueryImpl<R>
return this;
}
@Override
@SuppressWarnings("unchecked")
public NativeQueryImplementor<R> setParameter(QueryParameter parameter, Object value, Type type) {
super.setParameter( parameter, value, type );
return this;
}
@Override
public NativeQueryImplementor<R> setParameter(String name, Object value, Type type) {
super.setParameter( name, value, type );
return this;
}
@Override
public NativeQueryImplementor<R> setParameter(int position, Object value, Type type) {
super.setParameter( position, value, type );
return this;
}
// @Override
// @SuppressWarnings("unchecked")
// public NativeQueryImplementor<R> setParameter(QueryParameter parameter, Object value, Type type) {
// super.setParameter( parameter, value, type );
// return this;
// }
//
// @Override
// public NativeQueryImplementor<R> setParameter(String name, Object value, Type type) {
// super.setParameter( name, value, type );
// return this;
// }
//
// @Override
// public NativeQueryImplementor<R> setParameter(int position, Object value, Type type) {
// super.setParameter( position, value, type );
// return this;
// }
@Override
public <P> NativeQueryImplementor<R> setParameter(QueryParameter<P> parameter, P value, TemporalType temporalType) {
@ -892,11 +892,11 @@ public class NativeQueryImpl<R>
super.setParameter( param, value, temporalType );
return this;
}
@Override
public NativeQueryImplementor<R> setParameterList(int position, Collection values, Type type) {
return setParameterList( position, values, ( AllowableParameterType) type );
}
//
// @Override
// public NativeQueryImplementor<R> setParameterList(int position, Collection values, Type type) {
// return setParameterList( position, values, ( AllowableParameterType) type );
// }
@Override
public NativeQueryImplementor<R> setParameterList(String name, Collection values, AllowableParameterType type) {
@ -922,10 +922,10 @@ public class NativeQueryImpl<R>
return (NativeQueryImplementor) super.setParameterList( position, values, type );
}
@Override
public NativeQueryImplementor<R> setParameterList(int position, Object[] values, Type type) {
return null;
}
// @Override
// public NativeQueryImplementor<R> setParameterList(int position, Object[] values, Type type) {
// return null;
// }
@Override
public NativeQueryImplementor<R> setParameter(String name, Instant value, TemporalType temporalType) {
@ -987,17 +987,17 @@ public class NativeQueryImpl<R>
return this;
}
@Override
public NativeQueryImplementor<R> setParameterList(String name, Collection values, Type type) {
super.setParameterList( name, values, type );
return this;
}
@Override
public NativeQueryImplementor<R> setParameterList(String name, Object[] values, Type type) {
super.setParameterList( name, values, type );
return this;
}
// @Override
// public NativeQueryImplementor<R> setParameterList(String name, Collection values, Type type) {
// super.setParameterList( name, values, type );
// return this;
// }
//
// @Override
// public NativeQueryImplementor<R> setParameterList(String name, Object[] values, Type type) {
// super.setParameterList( name, values, type );
// return this;
// }
@Override
public NativeQueryImplementor<R> setParameterList(String name, Object[] values) {

View File

@ -34,7 +34,6 @@ import org.hibernate.query.ResultListTransformer;
import org.hibernate.query.TupleTransformer;
import org.hibernate.query.spi.NameableQuery;
import org.hibernate.query.spi.QueryImplementor;
import org.hibernate.type.Type;
/**
* @author Steve Ebersole
@ -159,10 +158,10 @@ public interface NativeQueryImplementor<R> extends QueryImplementor<R>, NativeQu
@Override
NativeQueryImplementor<R> setParameter(String name, Object val);
@Override
default <P> NativeQueryImplementor<R> setParameter(QueryParameter<P> parameter, P val, Type type) {
return setParameter( parameter, val, (AllowableParameterType) type );
}
// @Override
// default <P> NativeQueryImplementor<R> setParameter(QueryParameter<P> parameter, P val, Type type) {
// return setParameter( parameter, val, (AllowableParameterType) type );
// }
@Override
<P> NativeQueryImplementor<R> setParameter(QueryParameter<P> parameter, P val, AllowableParameterType type);
@ -170,18 +169,18 @@ public interface NativeQueryImplementor<R> extends QueryImplementor<R>, NativeQu
@Override
NativeQueryImplementor<R> setParameter(int position, Object val);
@Override
default NativeQueryImplementor<R> setParameter(String name, Object val, Type type) {
return setParameter( name, val, (AllowableParameterType) type );
}
// @Override
// default NativeQueryImplementor<R> setParameter(String name, Object val, Type type) {
// return setParameter( name, val, (AllowableParameterType) type );
// }
@Override
NativeQueryImplementor<R> setParameter(String name, Object val, AllowableParameterType type);
@Override
default NativeQueryImplementor<R> setParameter(int position, Object val, Type type) {
return setParameter( position, val, (AllowableParameterType) type );
}
// @Override
// default NativeQueryImplementor<R> setParameter(int position, Object val, Type type) {
// return setParameter( position, val, (AllowableParameterType) type );
// }
@Override
NativeQueryImplementor<R> setParameter(int position, Object val, AllowableParameterType type);
@ -206,18 +205,18 @@ public interface NativeQueryImplementor<R> extends QueryImplementor<R>, NativeQu
@Override
NativeQueryImplementor<R> setParameterList(String name, Collection values);
@Override
default NativeQueryImplementor<R> setParameterList(String name, Collection values, Type type) {
return setParameterList( name, values, (AllowableParameterType) type );
}
// @Override
// default NativeQueryImplementor<R> setParameterList(String name, Collection values, Type type) {
// return setParameterList( name, values, (AllowableParameterType) type );
// }
@Override
NativeQueryImplementor<R> setParameterList(String name, Collection values, AllowableParameterType type);
@Override
default NativeQueryImplementor<R> setParameterList(String name, Object[] values, Type type) {
return setParameterList( name, values, (AllowableParameterType) type );
}
//
// @Override
// default NativeQueryImplementor<R> setParameterList(String name, Object[] values, Type type) {
// return setParameterList( name, values, (AllowableParameterType) type );
// }
@Override
NativeQueryImplementor<R> setParameterList(String name, Object[] values, AllowableParameterType type);
@ -312,14 +311,14 @@ public interface NativeQueryImplementor<R> extends QueryImplementor<R>, NativeQu
@Override
NativeQueryImplementor<R> setParameterList(int position, Collection values, Class type);
@Override
NativeQueryImplementor<R> setParameterList(int position, Collection values, Type type);
// @Override
// NativeQueryImplementor<R> setParameterList(int position, Collection values, Type type);
@Override
NativeQueryImplementor<R> setParameterList(int position, Collection values, AllowableParameterType type);
@Override
NativeQueryImplementor<R> setParameterList(int position, Object[] values, Type type);
// @Override
// NativeQueryImplementor<R> setParameterList(int position, Object[] values, Type type);
@Override
NativeQueryImplementor<R> setParameterList(int position, Object[] values, AllowableParameterType type);

View File

@ -149,28 +149,6 @@ public class NativeSQLQueriesTest extends BaseCoreFunctionalTestCase {
s.close();
}
}
@Test
public void testRegisteredNamedSQLQueryWithScalar()
{
final NamedNativeQueryMementoBuilder builder = new NamedNativeQueryMementoBuilder();
builder.setName("namedQuery");
builder.setQuery("select count(*) AS c from ORGANIZATION");
builder.setQueryReturns(new NativeSQLQueryReturn[1]);
sessionFactory().registerNamedSQLQueryDefinition("namedQuery", builder.createNamedQueryDefinition());
final Session s = openSession();
s.beginTransaction();
final NativeQuery query = (NativeQuery) s.getNamedQuery("namedQuery");
query.addScalar("c");
final Number result = (Number) query.uniqueResult();
s.getTransaction().commit();
s.close();
assertNotNull(result);
assertTrue(0 == result.intValue());
}
@Test
public void testManualSynchronization() {

View File

@ -0,0 +1,56 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
*/
package org.hibernate.test.sql.hand.query;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;
import org.hibernate.engine.query.spi.sql.NativeSQLQueryReturn;
import org.hibernate.query.NativeQuery;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Test;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
/**
* @author Andrea Boriero
*/
public class NamedNativeQueryMementoBuilderTest extends BaseCoreFunctionalTestCase {
public String[] getMappings() {
return new String[] { "sql/hand/query/NativeSQLQueries.hbm.xml" };
}
@Override
public void configure(Configuration cfg) {
super.configure( cfg );
cfg.setProperty( Environment.GENERATE_STATISTICS, "true" );
}
@Test
public void testRegisteredNamedSQLQueryWithScalar()
{
final NamedNativeQueryMementoBuilder builder = new NamedNativeQueryMementoBuilder();
builder.setName("namedQuery");
builder.setQuery("select count(*) AS c from ORGANIZATION");
builder.setQueryReturns(new NativeSQLQueryReturn[1]);
sessionFactory().registerNamedSQLQueryDefinition("namedQuery", builder.createNamedQueryDefinition());
final Session s = openSession();
s.beginTransaction();
final NativeQuery query = (NativeQuery) s.getNamedQuery( "namedQuery");
query.addScalar("c");
final Number result = (Number) query.uniqueResult();
s.getTransaction().commit();
s.close();
assertNotNull(result);
assertTrue(0 == result.intValue());
}
}