|
12 | 12 | import org.hibernate.metamodel.mapping.EntityAssociationMapping; |
13 | 13 | import org.hibernate.metamodel.mapping.ForeignKeyDescriptor; |
14 | 14 | import org.hibernate.metamodel.mapping.ModelPart; |
| 15 | +import org.hibernate.metamodel.mapping.ModelPartContainer; |
15 | 16 | import org.hibernate.metamodel.mapping.PluralAttributeMapping; |
| 17 | +import org.hibernate.metamodel.mapping.TableDetails; |
16 | 18 | import org.hibernate.metamodel.mapping.ValuedModelPart; |
17 | 19 | import org.hibernate.metamodel.mapping.internal.BasicValuedCollectionPart; |
18 | 20 | import org.hibernate.persister.entity.EntityPersister; |
| 21 | +import org.hibernate.persister.entity.mutation.EntityTableMapping; |
19 | 22 | import org.hibernate.sql.ast.SqlAstJoinType; |
20 | 23 | import org.hibernate.sql.ast.spi.LockingClauseStrategy; |
21 | 24 | import org.hibernate.sql.ast.spi.SqlAppender; |
| 25 | +import org.hibernate.sql.ast.tree.from.NamedTableReference; |
22 | 26 | import org.hibernate.sql.ast.tree.from.TableGroup; |
23 | 27 | import org.hibernate.sql.ast.tree.from.TableGroupJoin; |
24 | 28 | import org.hibernate.sql.ast.tree.from.TableReferenceJoin; |
| 29 | +import org.hibernate.sql.model.TableMapping; |
25 | 30 |
|
26 | 31 | import java.util.ArrayList; |
27 | | -import java.util.Collections; |
28 | 32 | import java.util.HashSet; |
29 | 33 | import java.util.LinkedHashSet; |
30 | 34 | import java.util.List; |
@@ -212,21 +216,54 @@ private void addTableAliases(TableGroup tableGroup, List<String> lockItems) { |
212 | 216 | } |
213 | 217 |
|
214 | 218 | private void addColumnRefs(TableGroup tableGroup, List<String> lockItems) { |
215 | | - Collections.addAll( lockItems, determineKeyColumnRefs( tableGroup ) ); |
216 | | - } |
217 | | - |
218 | | - private String[] determineKeyColumnRefs(TableGroup tableGroup) { |
219 | 219 | final String[] keyColumns = determineKeyColumnNames( tableGroup.getModelPart() ); |
220 | | - final String[] result = new String[keyColumns.length]; |
221 | 220 | final String tableAlias = tableGroup.getPrimaryTableReference().getIdentificationVariable(); |
222 | 221 | for ( int i = 0; i < keyColumns.length; i++ ) { |
223 | 222 | // NOTE: in some tests with Oracle, the qualifiers are being applied twice; |
224 | 223 | // still need to track that down. possibly, unexpected calls to |
225 | 224 | // `Dialect#applyLocksToSql`? |
226 | 225 | assert !keyColumns[i].contains( "." ); |
227 | | - result[i] = tableAlias + "." + keyColumns[i]; |
| 226 | + lockItems.add( tableAlias + "." + keyColumns[i] ); |
| 227 | + } |
| 228 | + |
| 229 | + final List<TableReferenceJoin> tableReferenceJoins = tableGroup.getTableReferenceJoins(); |
| 230 | + if ( CollectionHelper.isNotEmpty( tableReferenceJoins ) ) { |
| 231 | + final EntityPersister entityPersister = determineEntityPersister( tableGroup.getModelPart() ); |
| 232 | + for ( int i = 0; i < tableReferenceJoins.size(); i++ ) { |
| 233 | + final TableReferenceJoin tableReferenceJoin = tableReferenceJoins.get( i ); |
| 234 | + final NamedTableReference joinedTableReference = tableReferenceJoin.getJoinedTableReference(); |
| 235 | + final String tableJoinAlias = joinedTableReference.getIdentificationVariable(); |
| 236 | + final TableMapping tableMapping = determineTableMapping( entityPersister, tableReferenceJoin ); |
| 237 | + for ( TableDetails.KeyColumn keyColumn : tableMapping.getKeyDetails().getKeyColumns() ) { |
| 238 | + lockItems.add( tableJoinAlias + "." + keyColumn.getColumnName() ); |
| 239 | + } |
| 240 | + } |
| 241 | + } |
| 242 | + } |
| 243 | + |
| 244 | + private TableMapping determineTableMapping(EntityPersister entityPersister, TableReferenceJoin tableReferenceJoin) { |
| 245 | + final NamedTableReference joinedTableReference = tableReferenceJoin.getJoinedTableReference(); |
| 246 | + for ( EntityTableMapping tableMapping : entityPersister.getTableMappings() ) { |
| 247 | + if ( joinedTableReference.containsAffectedTableName( tableMapping.getTableName() ) ) { |
| 248 | + return tableMapping; |
| 249 | + } |
| 250 | + } |
| 251 | + throw new IllegalArgumentException( "Couldn't find subclass index for joined table reference " + joinedTableReference ); |
| 252 | + } |
| 253 | + |
| 254 | + private EntityPersister determineEntityPersister(ModelPartContainer modelPart) { |
| 255 | + if ( modelPart instanceof EntityPersister entityPersister ) { |
| 256 | + return entityPersister; |
| 257 | + } |
| 258 | + else if ( modelPart instanceof PluralAttributeMapping pluralAttributeMapping ) { |
| 259 | + return pluralAttributeMapping.getCollectionDescriptor().getElementPersister(); |
| 260 | + } |
| 261 | + else if ( modelPart instanceof EntityAssociationMapping entityAssociationMapping ) { |
| 262 | + return entityAssociationMapping.getAssociatedEntityMappingType().getEntityPersister(); |
| 263 | + } |
| 264 | + else { |
| 265 | + throw new IllegalArgumentException( "Expected table group with table joins to have an entity typed model part but got: " + modelPart ); |
228 | 266 | } |
229 | | - return result; |
230 | 267 | } |
231 | 268 |
|
232 | 269 | private String[] determineKeyColumnNames(ModelPart modelPart) { |
|
0 commit comments