fix signature of Query.setOrder()

This commit is contained in:
Gavin King 2024-11-13 23:05:56 +01:00
parent 538cad670f
commit 9d30d210ed
16 changed files with 54 additions and 63 deletions

View File

@ -413,7 +413,7 @@ public class ProcedureCallImpl<R>
}
@Override
public Query<R> setOrder(List<Order<? super R>> orderList) {
public Query<R> setOrder(List<? extends Order<? super R>> orderList) {
throw new UnsupportedOperationException("Ordering not supported for stored procedure calls");
}

View File

@ -922,7 +922,7 @@ public interface Query<R> extends SelectionQuery<R>, MutationQuery, TypedQuery<R
Query<R> setLockMode(LockModeType lockMode);
@Override @Incubating
Query<R> setOrder(List<Order<? super R>> orderList);
Query<R> setOrder(List<? extends Order<? super R>> orderList);
@Override @Incubating
Query<R> setOrder(Order<? super R> order);

View File

@ -590,7 +590,7 @@ public interface SelectionQuery<R> extends CommonQueryContract {
* @since 6.3
*/
@Incubating
SelectionQuery<R> setOrder(List<Order<? super R>> orderList);
SelectionQuery<R> setOrder(List<? extends Order<? super R>> orderList);
/**
* If the result type of this query is an entity class, add a

View File

@ -295,7 +295,7 @@ public abstract class AbstractQuery<R>
}
@Override
public Query<R> setOrder(List<Order<? super R>> orders) {
public Query<R> setOrder(List<? extends Order<? super R>> orders) {
throw new UnsupportedOperationException( "Should be implemented by " + this.getClass().getName() );
}

View File

@ -1581,7 +1581,7 @@ public class NativeQueryImpl<R>
}
@Override
public Query<R> setOrder(List<Order<? super R>> orderList) {
public Query<R> setOrder(List<? extends Order<? super R>> orderList) {
throw new UnsupportedOperationException("Ordering not currently supported for native queries");
}

View File

@ -122,29 +122,26 @@ abstract class AbstractSqmSelectionQuery<R> extends AbstractSelectionQuery<R> {
}
@Override
public SelectionQuery<R> setOrder(List<Order<? super R>> orderList) {
SqmSelectStatement<R> sqm = getSqmSelectStatement();
sqm = sqm.copy( noParamCopyContext() );
final SqmSelectStatement<R> select = sqm;
sqm.orderBy( orderList.stream().map( order -> sortSpecification( select, order ) )
public SelectionQuery<R> setOrder(List<? extends Order<? super R>> orderList) {
final SqmSelectStatement<R> selectStatement = getSqmSelectStatement().copy( noParamCopyContext() );
selectStatement.orderBy( orderList.stream().map( order -> sortSpecification( selectStatement, order ) )
.collect( toList() ) );
// TODO: when the QueryInterpretationCache can handle caching criteria queries,
// simply cache the new SQM as if it were a criteria query, and remove this:
getQueryOptions().setQueryPlanCachingEnabled( false );
setSqmStatement( sqm );
setSqmStatement( selectStatement );
return this;
}
@Override
public SelectionQuery<R> setOrder(Order<? super R> order) {
SqmSelectStatement<R> sqm = getSqmSelectStatement();
sqm = sqm.copy( noParamCopyContext() );
sqm.orderBy( sortSpecification( sqm, order ) );
final SqmSelectStatement<R> selectStatement = getSqmSelectStatement().copy( noParamCopyContext() );
selectStatement.orderBy( sortSpecification( selectStatement, order ) );
// TODO: when the QueryInterpretationCache can handle caching criteria queries,
// simply cache the new SQM as if it were a criteria query, and remove this:
getQueryOptions().setQueryPlanCachingEnabled( false );
setSqmStatement( sqm );
setSqmStatement( selectStatement );
return this;
}

View File

@ -758,7 +758,7 @@ public class QuerySqmImpl<R>
}
@Override
public Query<R> setOrder(List<Order<? super R>> orders) {
public Query<R> setOrder(List<? extends Order<? super R>> orders) {
super.setOrder(orders);
return this;
}

View File

@ -295,7 +295,7 @@ public abstract class DelegatingSqmSelectionQueryImplementor<R> implements SqmSe
@Override
@Incubating
public SqmSelectionQueryImplementor<R> setOrder(List<Order<? super R>> orders) {
public SqmSelectionQueryImplementor<R> setOrder(List<? extends Order<? super R>> orders) {
getDelegate().setOrder( orders );
return this;
}

View File

@ -7,6 +7,7 @@ package org.hibernate.orm.test.jpa.ejb3configuration;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import org.hibernate.HibernateException;
@ -216,6 +217,13 @@ public class ConfigurationObjectSettingTest {
);
}
public static void applyToProperties(Properties properties, Object... pairs) {
assert pairs.length % 2 == 0;
for ( int i = 0; i < pairs.length; i+=2 ) {
properties.put( pairs[i], pairs[i+1] );
}
}
private void verifyJdbcSettings(String jdbcUrl, String jdbcDriver, String jdbcUser, String jdbcPassword) {
final String urlValue = "some:url";
final String driverValue = "some.jdbc.Driver";
@ -228,7 +236,7 @@ public class ConfigurationObjectSettingTest {
{
builder = (EntityManagerFactoryBuilderImpl) Bootstrap.getEntityManagerFactoryBuilder(
empty,
CollectionHelper.toMap(
Map.of(
jdbcUrl, urlValue,
jdbcDriver, driverValue,
jdbcUser, userValue,
@ -246,7 +254,7 @@ public class ConfigurationObjectSettingTest {
}
PersistenceUnitInfoAdapter pui = new PersistenceUnitInfoAdapter();
CollectionHelper.applyToProperties(
applyToProperties(
pui.getProperties(),
jdbcUrl, urlValue,
jdbcDriver, driverValue,

View File

@ -13,7 +13,6 @@ import org.hibernate.cfg.AvailableSettings;
import org.hibernate.cfg.Environment;
import org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.internal.util.collections.CollectionHelper;
import org.hibernate.jpa.boot.spi.Bootstrap;
import org.hibernate.jpa.boot.spi.EntityManagerFactoryBuilder;
import org.hibernate.tool.schema.Action;
@ -137,6 +136,13 @@ public class JakartaSchemaToolingTests {
}
}
public static void applyToProperties(Properties properties, Object... pairs) {
assert pairs.length % 2 == 0;
for ( int i = 0; i < pairs.length; i+=2 ) {
properties.put( pairs[i], pairs[i+1] );
}
}
private SessionFactoryImplementor buildSessionFactory(Object... settingPairs) {
final Properties settings = new Properties();
settings.setProperty( AvailableSettings.AUTOCOMMIT, "false" );
@ -146,7 +152,7 @@ public class JakartaSchemaToolingTests {
DriverManagerConnectionProviderImpl.INIT_SQL,
Environment.getProperties().getProperty( DriverManagerConnectionProviderImpl.INIT_SQL )
);
CollectionHelper.applyToProperties( settings, settingPairs );
applyToProperties( settings, settingPairs );
ServiceRegistryUtil.applySettings( settings );
final PersistenceUnitDescriptorAdapter puDescriptor = new PersistenceUnitDescriptorAdapter() {

View File

@ -6,13 +6,13 @@ package org.hibernate.orm.test.mapping.fetch.depth;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import jakarta.persistence.EntityManagerFactory;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.internal.util.collections.CollectionHelper;
import org.hibernate.tool.schema.Action;
import org.hibernate.testing.orm.junit.JiraKey;
@ -85,12 +85,12 @@ public class NoDepthTests {
par.addAsResource( "units/many2many/fetch-depth.xml", "META-INF/persistence.xml" );
try ( final ShrinkWrapClassLoader classLoader = new ShrinkWrapClassLoader( par ) ) {
final Map<String, ?> settings = CollectionHelper.toMap(
final Map<String, Object> settings = new HashMap<>( Map.of(
CLASSLOADERS, Arrays.asList( classLoader, getClass().getClassLoader() ),
MAX_FETCH_DEPTH, configureMax ? "10" : "",
HBM2DDL_AUTO, Action.CREATE_DROP,
FORMAT_SQL, "true"
);
) );
ServiceRegistryUtil.applySettings( settings );
final EntityManagerFactory emf = createEntityManagerFactory( "fetch-depth", settings );

View File

@ -6,28 +6,25 @@ package org.hibernate.orm.test.mapping.mutability.converted;
import java.util.Map;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.internal.util.collections.CollectionHelper;
import jakarta.persistence.AttributeConverter;
import static org.hibernate.internal.util.StringHelper.isEmpty;
import static org.hibernate.internal.util.StringHelper.join;
import static org.hibernate.internal.util.StringHelper.split;
/**
* @author Steve Ebersole
*/
public class MapConverter implements AttributeConverter<Map<String, String>, String> {
@Override
public String convertToDatabaseColumn(Map<String, String> map) {
if ( CollectionHelper.isEmpty( map ) ) {
return null;
}
return StringHelper.join( ", ", CollectionHelper.asPairs( map ) );
return CollectionHelper.isEmpty( map ) ? null : join( ", ", CollectionHelper.asPairs( map ) );
}
@Override
public Map<String, String> convertToEntityAttribute(String pairs) {
if ( StringHelper.isEmpty( pairs ) ) {
return null;
}
return CollectionHelper.toMap( StringHelper.split( ", ", pairs ) );
return isEmpty( pairs ) ? null : CollectionHelper.toMap( split( ", ", pairs ) );
}
}

View File

@ -24,7 +24,6 @@ import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static org.hibernate.internal.util.collections.CollectionHelper.toSettingsMap;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
@ -60,14 +59,8 @@ public class WhereFragmentTests {
* Loads a User, fetching their detail and skills using an entity-graph
*/
public User findUserByIdUsingEntityGraph(Integer id, SessionFactoryScope factoryScope) {
return factoryScope.fromTransaction( (session) -> {
final Map<String, Object> properties = toSettingsMap(
SpecHints.HINT_SPEC_FETCH_GRAPH,
session.getEntityGraph("user-entity-graph")
);
return session.find(User.class, id, properties);
} );
return factoryScope.fromTransaction( (session) -> session.find( User.class, id,
Map.of( SpecHints.HINT_SPEC_FETCH_GRAPH, session.getEntityGraph("user-entity-graph") ) ) );
}
/**

View File

@ -6,7 +6,6 @@ package org.hibernate.orm.test.query.hql;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -25,8 +24,8 @@ import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static java.util.Collections.singletonMap;
import static org.hamcrest.Matchers.is;
import static org.hibernate.internal.util.collections.CollectionHelper.toMap;
import static org.junit.Assert.assertThat;
/**
@ -57,13 +56,11 @@ public class LegacyParameterTests {
scope.inTransaction(
(s) -> {
Map<String,String> parameters = toMap( "nickName", null );
Query<Human> q = s.createQuery(
"from Human h where h.nickName = :nickName or (h.nickName is null and :nickName is null)",
Human.class
);
q.setProperties( (parameters) );
q.setProperties( singletonMap( "nickName", null ) );
assertThat( q.list().size(), is( 0 ) );
Human human1 = new Human();
@ -71,20 +68,14 @@ public class LegacyParameterTests {
human1.setNickName( null );
s.persist( human1 );
parameters = new HashMap<>();
parameters.put( "nickName", null );
q = s.createQuery( "from Human h where h.nickName = :nickName or (h.nickName is null and :nickName is null)", Human.class );
q.setProperties( (parameters) );
q.setProperties( singletonMap( "nickName", null ) );
assertThat( q.list().size(), is( 1 ) );
Human found = q.list().get( 0 );
assertThat( found.getId(), is( human1.getId() ) );
parameters = new HashMap<>();
parameters.put( "nickName", "nick" );
q = s.createQuery( "from Human h where h.nickName = :nickName or (h.nickName is null and :nickName is null)", Human.class );
q.setProperties( (parameters) );
q.setProperties( singletonMap( "nickName", "nick" ) );
assertThat( q.list().size(), is( 1 ) );
found = q.list().get( 0 );
assertThat( found.getId(), is( 1L ) );
@ -97,8 +88,6 @@ public class LegacyParameterTests {
public void testSetPropertiesMapNotContainingAllTheParameters(SessionFactoryScope scope) {
scope.inTransaction(
(s) -> {
Map<String,String> parameters = toMap( "nickNames", "nick" );
List<Integer> intValues = new ArrayList<>();
intValues.add( 1 );
//noinspection unchecked
@ -106,7 +95,7 @@ public class LegacyParameterTests {
"from Human h where h.nickName in (:nickNames) and h.intValue in (:intValues)"
);
q.setParameterList( "intValues" , intValues);
q.setProperties( (parameters) );
q.setProperties( Map.of( "nickNames", "nick" ) );
assertThat( q.list().size(), is( 1 ) );
}
);

View File

@ -40,7 +40,6 @@ import jakarta.persistence.EntityManagerFactory;
import static org.assertj.core.api.Assertions.assertThat;
import static org.hibernate.cfg.AvailableSettings.JAKARTA_HBM2DDL_DATABASE_ACTION;
import static org.hibernate.internal.util.collections.CollectionHelper.toMap;
import static org.hibernate.jpa.HibernateHints.HINT_TENANT_ID;
import static org.junit.Assert.assertNull;
import static org.junit.jupiter.api.Assertions.assertEquals;
@ -50,6 +49,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import java.util.List;
import java.util.Map;
@SessionFactory
@DomainModel(annotatedClasses = { Account.class, Client.class, Record.class })
@ -254,7 +254,7 @@ public class TenantIdTest implements SessionFactoryProducer {
currentTenant = null;
final EntityManagerFactory emf = scope.getSessionFactory();
try (EntityManager em = emf.createEntityManager( toMap( HINT_TENANT_ID, "mine" ) ) ) {
try (EntityManager em = emf.createEntityManager( Map.of( HINT_TENANT_ID, "mine" ) ) ) {
Record r = em.find( Record.class, record.id );
assertEquals( "mine", r.state.tenantId );

View File

@ -46,12 +46,13 @@ public class ServiceRegistryUtil {
public static void applySettings(Map<?, ?> properties) {
if ( !properties.containsKey( AvailableSettings.CONNECTION_PROVIDER ) ) {
//noinspection unchecked
( (Map<Object, Object>) properties ).put(
@SuppressWarnings( "unchecked" )
final Map<Object, Object> objectMap = (Map<Object, Object>) properties;
objectMap.put(
AvailableSettings.CONNECTION_PROVIDER,
SharedDriverManagerConnectionProviderImpl.getInstance()
);
( (Map<Object, Object>) properties ).put(
objectMap.put(
AvailableSettings.CONNECTION_PROVIDER_DISABLES_AUTOCOMMIT,
Boolean.TRUE
);