|
7 | 7 | import java.sql.SQLException; |
8 | 8 | import java.util.ArrayList; |
9 | 9 | import java.util.List; |
| 10 | +import java.util.Objects; |
| 11 | +import java.util.stream.Collectors; |
10 | 12 |
|
11 | 13 | import org.hibernate.Internal; |
12 | 14 | import org.hibernate.dialect.Dialect; |
|
19 | 21 | import org.hibernate.engine.jdbc.mutation.group.PreparedStatementDetails; |
20 | 22 | import org.hibernate.engine.spi.SessionFactoryImplementor; |
21 | 23 | import org.hibernate.engine.spi.SharedSessionContractImplementor; |
| 24 | +import org.hibernate.exception.MissingAttributeException; |
22 | 25 | import org.hibernate.generator.BeforeExecutionGenerator; |
23 | 26 | import org.hibernate.generator.Generator; |
24 | 27 | import org.hibernate.generator.OnExecutionGenerator; |
@@ -427,21 +430,28 @@ else if ( isValueGenerationInSql( generator, factory.getJdbcServices().getDialec |
427 | 430 | insertGroupBuilder.forEachTableMutationBuilder( (tableMutationBuilder) -> { |
428 | 431 | final TableInsertBuilder tableInsertBuilder = (TableInsertBuilder) tableMutationBuilder; |
429 | 432 | final EntityTableMapping tableMapping = (EntityTableMapping) tableInsertBuilder.getMutatingTable().getTableMapping(); |
| 433 | + EntityTableMapping.KeyMapping keyMapping = tableMapping.getKeyMapping(); |
430 | 434 | if ( tableMapping.isIdentifierTable() && entityPersister().isIdentifierAssignedByInsert() && !forceIdentifierBinding ) { |
431 | 435 | assert entityPersister().getInsertDelegate() != null; |
432 | 436 | final OnExecutionGenerator generator = (OnExecutionGenerator) entityPersister().getGenerator(); |
433 | 437 | if ( generator.referenceColumnsInSql( dialect ) ) { |
434 | 438 | final BasicEntityIdentifierMapping identifierMapping = (BasicEntityIdentifierMapping) entityPersister().getIdentifierMapping(); |
435 | 439 | final String[] columnValues = generator.getReferencedColumnValues( dialect ); |
436 | | - tableMapping.getKeyMapping().forEachKeyColumn( (i, column) -> tableInsertBuilder.addKeyColumn( |
| 440 | + if ( columnValues == null ) { |
| 441 | + // We cannot handle any further |
| 442 | + throw new MissingAttributeException( "Column value is missing. Could not generate value for " + keyMapping.getKeyColumns() |
| 443 | + .stream().map( Objects::toString ).collect( |
| 444 | + Collectors.joining( ", " ) ) ); |
| 445 | + } |
| 446 | + keyMapping.forEachKeyColumn( (i, column) -> tableInsertBuilder.addKeyColumn( |
437 | 447 | column.getColumnName(), |
438 | 448 | columnValues[i], |
439 | 449 | identifierMapping.getJdbcMapping() |
440 | 450 | ) ); |
441 | 451 | } |
442 | 452 | } |
443 | 453 | else { |
444 | | - tableMapping.getKeyMapping().forEachKeyColumn( tableInsertBuilder::addKeyColumn ); |
| 454 | + keyMapping.forEachKeyColumn( tableInsertBuilder::addKeyColumn ); |
445 | 455 | } |
446 | 456 | } ); |
447 | 457 | } |
|
0 commit comments