Skip to content

Commit c006b06

Browse files
committed
HHH-18679 throw exception when columnValues is null while addKeyColumn (id)
Signed-off-by: Bao.Ngo <[email protected]>
1 parent 7afb5a0 commit c006b06

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* SPDX-License-Identifier: LGPL-2.1-or-later
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.exception;
6+
7+
import org.hibernate.HibernateException;
8+
9+
/**
10+
* An attribute required, but it is missing.
11+
* There are a number of possible underlying causes, including:
12+
* <ul>
13+
* <li>Missing annotation attribute,
14+
* <li>Missing configuration
15+
* </ul>
16+
*
17+
* @author Bao Ngo
18+
*/
19+
public class MissingAttributeException extends HibernateException {
20+
21+
/**
22+
* Constructs a {@code MissingAttributeException} using the given exception message.
23+
*
24+
* @param message The message explaining the reason for the exception
25+
*/
26+
public MissingAttributeException(String message) {
27+
super( message );
28+
}
29+
}

hibernate-core/src/main/java/org/hibernate/persister/entity/mutation/EntityTableMapping.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,5 +410,10 @@ public String getCustomReadExpression() {
410410
public String getCustomWriteExpression() {
411411
return null;
412412
}
413+
414+
@Override
415+
public String toString() {
416+
return "{%s - %s}".formatted( tableName, columnName );
417+
}
413418
}
414419
}

hibernate-core/src/main/java/org/hibernate/persister/entity/mutation/InsertCoordinatorStandard.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import java.sql.SQLException;
88
import java.util.ArrayList;
99
import java.util.List;
10+
import java.util.Objects;
11+
import java.util.stream.Collectors;
1012

1113
import org.hibernate.Internal;
1214
import org.hibernate.dialect.Dialect;
@@ -19,6 +21,7 @@
1921
import org.hibernate.engine.jdbc.mutation.group.PreparedStatementDetails;
2022
import org.hibernate.engine.spi.SessionFactoryImplementor;
2123
import org.hibernate.engine.spi.SharedSessionContractImplementor;
24+
import org.hibernate.exception.MissingAttributeException;
2225
import org.hibernate.generator.BeforeExecutionGenerator;
2326
import org.hibernate.generator.Generator;
2427
import org.hibernate.generator.OnExecutionGenerator;
@@ -427,21 +430,28 @@ else if ( isValueGenerationInSql( generator, factory.getJdbcServices().getDialec
427430
insertGroupBuilder.forEachTableMutationBuilder( (tableMutationBuilder) -> {
428431
final TableInsertBuilder tableInsertBuilder = (TableInsertBuilder) tableMutationBuilder;
429432
final EntityTableMapping tableMapping = (EntityTableMapping) tableInsertBuilder.getMutatingTable().getTableMapping();
433+
EntityTableMapping.KeyMapping keyMapping = tableMapping.getKeyMapping();
430434
if ( tableMapping.isIdentifierTable() && entityPersister().isIdentifierAssignedByInsert() && !forceIdentifierBinding ) {
431435
assert entityPersister().getInsertDelegate() != null;
432436
final OnExecutionGenerator generator = (OnExecutionGenerator) entityPersister().getGenerator();
433437
if ( generator.referenceColumnsInSql( dialect ) ) {
434438
final BasicEntityIdentifierMapping identifierMapping = (BasicEntityIdentifierMapping) entityPersister().getIdentifierMapping();
435439
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(
437447
column.getColumnName(),
438448
columnValues[i],
439449
identifierMapping.getJdbcMapping()
440450
) );
441451
}
442452
}
443453
else {
444-
tableMapping.getKeyMapping().forEachKeyColumn( tableInsertBuilder::addKeyColumn );
454+
keyMapping.forEachKeyColumn( tableInsertBuilder::addKeyColumn );
445455
}
446456
} );
447457
}

0 commit comments

Comments
 (0)