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