From e9ba27e88fb6842247b3254be0ec5ded8bbe14ae Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Tue, 14 Mar 2023 13:27:46 +0100 Subject: [PATCH] HHH-16297 Removing an element from a collection of elements removes the whole collection --- .../internal/MappingModelCreationHelper.java | 1 + .../internal/SelectableMappingImpl.java | 39 +++++++++++++++---- .../AbstractCollectionPersister.java | 15 ------- 3 files changed, 33 insertions(+), 22 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/MappingModelCreationHelper.java b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/MappingModelCreationHelper.java index ccf7ad2be1..18bf541428 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/MappingModelCreationHelper.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/MappingModelCreationHelper.java @@ -1337,6 +1337,7 @@ public class MappingModelCreationHelper { basicElement.isColumnInsertable( 0 ), basicElement.isColumnUpdateable( 0 ), basicElement.isPartitionKey(), + true, // element collection does not support null elements dialect, creationProcess.getSqmFunctionRegistry() ); diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/SelectableMappingImpl.java b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/SelectableMappingImpl.java index b631bc48cd..f319cc7864 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/SelectableMappingImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/SelectableMappingImpl.java @@ -9,14 +9,12 @@ package org.hibernate.metamodel.mapping.internal; import java.util.Locale; import org.hibernate.dialect.Dialect; -import org.hibernate.mapping.BasicValue; import org.hibernate.mapping.Column; import org.hibernate.mapping.Selectable; import org.hibernate.metamodel.mapping.JdbcMapping; import org.hibernate.metamodel.mapping.SelectableMapping; import org.hibernate.metamodel.mapping.SelectablePath; import org.hibernate.query.sqm.function.SqmFunctionRegistry; -import org.hibernate.type.BasicType; import org.hibernate.type.spi.TypeConfiguration; /** @@ -90,6 +88,32 @@ public class SelectableMappingImpl extends SqlTypedMappingImpl implements Select ); } + public static SelectableMapping from( + final String containingTableExpression, + final Selectable selectable, + final JdbcMapping jdbcMapping, + final TypeConfiguration typeConfiguration, + boolean insertable, + boolean updateable, + boolean partitioned, + boolean forceNotNullable, + final Dialect dialect, + final SqmFunctionRegistry sqmFunctionRegistry) { + return from( + containingTableExpression, + selectable, + null, + jdbcMapping, + typeConfiguration, + insertable, + updateable, + partitioned, + forceNotNullable, + dialect, + sqmFunctionRegistry + ); + } + public static SelectableMapping from( final String containingTableExpression, final Selectable selectable, @@ -105,14 +129,12 @@ public class SelectableMappingImpl extends SqlTypedMappingImpl implements Select containingTableExpression, selectable, parentPath, - selectable instanceof Column - ? ( (Column) selectable ).getQuotedName( dialect ) - : selectable.getText(), jdbcMapping, typeConfiguration, insertable, updateable, partitioned, + false, dialect, sqmFunctionRegistry ); @@ -122,12 +144,12 @@ public class SelectableMappingImpl extends SqlTypedMappingImpl implements Select final String containingTableExpression, final Selectable selectable, final SelectablePath parentPath, - final String selectableName, final JdbcMapping jdbcMapping, final TypeConfiguration typeConfiguration, boolean insertable, boolean updateable, boolean partitioned, + boolean forceNotNullable, final Dialect dialect, final SqmFunctionRegistry sqmFunctionRegistry) { final String columnExpression; @@ -135,6 +157,7 @@ public class SelectableMappingImpl extends SqlTypedMappingImpl implements Select final Long length; final Integer precision; final Integer scale; + final String selectableName; final boolean isNullable; if ( selectable.isFormula() ) { columnExpression = selectable.getTemplate( dialect, typeConfiguration, sqmFunctionRegistry ); @@ -143,6 +166,7 @@ public class SelectableMappingImpl extends SqlTypedMappingImpl implements Select precision = null; scale = null; isNullable = true; + selectableName = selectable.getText(); } else { Column column = (Column) selectable; @@ -152,7 +176,8 @@ public class SelectableMappingImpl extends SqlTypedMappingImpl implements Select precision = column.getPrecision(); scale = column.getScale(); - isNullable = column.isNullable(); + isNullable = forceNotNullable ? false : column.isNullable(); + selectableName = column.getQuotedName( dialect ); } return new SelectableMappingImpl( containingTableExpression, diff --git a/hibernate-core/src/main/java/org/hibernate/persister/collection/AbstractCollectionPersister.java b/hibernate-core/src/main/java/org/hibernate/persister/collection/AbstractCollectionPersister.java index cae6f1b8d7..a417f08efa 100644 --- a/hibernate-core/src/main/java/org/hibernate/persister/collection/AbstractCollectionPersister.java +++ b/hibernate-core/src/main/java/org/hibernate/persister/collection/AbstractCollectionPersister.java @@ -10,7 +10,6 @@ import java.io.Serializable; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; -import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; @@ -193,7 +192,6 @@ public abstract class AbstractCollectionPersister protected final String[] elementFormulas; protected final boolean[] elementColumnIsGettable; protected final boolean[] elementColumnIsSettable; - protected final boolean[] elementColumnIsInPrimaryKey; protected final String[] indexColumnAliases; protected final String[] elementColumnAliases; protected final String[] keyColumnAliases; @@ -376,9 +374,7 @@ public abstract class AbstractCollectionPersister elementFormulas = new String[elementSpan]; elementColumnIsSettable = new boolean[elementSpan]; elementColumnIsGettable = new boolean[elementSpan]; - elementColumnIsInPrimaryKey = new boolean[elementSpan]; boolean isPureFormula = true; - boolean hasNotNullableColumns = false; boolean oneToMany = collectionBootDescriptor.isOneToMany(); boolean[] columnInsertability = null; if ( !oneToMany ) { @@ -415,23 +411,12 @@ public abstract class AbstractCollectionPersister // Preserves legacy non-@ElementCollection behavior elementColumnIsSettable[j] = true; } - elementColumnIsInPrimaryKey[j] = !col.isNullable(); - if ( !col.isNullable() ) { - hasNotNullableColumns = true; - } isPureFormula = false; } j++; } elementIsPureFormula = isPureFormula; - // workaround, for backward compatibility of sets with no - // not-null columns, assume all columns are used in the - // row locator SQL - if ( !hasNotNullableColumns ) { - Arrays.fill( elementColumnIsInPrimaryKey, true ); - } - // INDEX AND ROW SELECT hasIndex = collectionBootDescriptor.isIndexed();