Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.util.Properties;
import java.util.function.BiConsumer;

import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.LockOptions;
import org.hibernate.MappingException;
Expand Down Expand Up @@ -584,6 +585,11 @@ private IntegralDataTypeHolder nextValue(
else {
final int defaultValue = storeLastUsedValue ? 0 : 1;
value.initialize( selectRS, defaultValue );
if ( selectRS.wasNull() ) {
throw new HibernateException(
String.format( "%s for %s '%s' is null", valueColumnName, segmentColumnName,
segmentValue ) );
}
}
selectRS.close();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* SPDX-License-Identifier: Apache-2.0
* Copyright Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.orm.test.schemaupdate.idgenerator;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;

import jakarta.persistence.TableGenerator;
import org.hibernate.HibernateException;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.JiraKey;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

@JiraKey(value = "HHH-8535")
@DomainModel(annotatedClasses = {TableGeneratorNextValNullTest.Author.class})
@SessionFactory
public class TableGeneratorNextValNullTest {

@AfterAll
public void dropTestData(SessionFactoryScope scope) {
scope.dropData();
}

@Test
void hhh8535Test(SessionFactoryScope scope) {

// This situation can only happen via human being or bad migration/clone script.
// Simulate this record being updated post table generation.
scope.inTransaction( session -> {
session.createNativeMutationQuery(
"UPDATE generator SET next_val = null where sequence_name = 'Author'"
).executeUpdate();
} );

HibernateException hibernateException = assertThrows( HibernateException.class,
() -> scope.inTransaction( session -> {
Author author = new Author();
session.persist( author );
} ) );

assertEquals( "next_val for sequence_name 'Author' is null", hibernateException.getMessage() );
}

@Entity(name = "Author")
public static class Author {

@Id
@GeneratedValue(strategy = GenerationType.TABLE, generator = "generator")
@TableGenerator(name = "generator", table = "generator")
long id;
}

}