HHH-18288 - Subclasses SINGLE_TABLE Inheritance should not be allowed to define @Table

This commit is contained in:
Steve Ebersole 2024-07-22 11:55:54 -05:00
parent d938576fcc
commit 3bf82e6d82
2 changed files with 28 additions and 14 deletions

View File

@ -750,7 +750,8 @@ public class EntityBinder {
final String table;
final String catalog;
final UniqueConstraint[] uniqueConstraints;
if ( annotatedClass.isAnnotationPresent( jakarta.persistence.Table.class ) ) {
final boolean hasTableAnnotation = annotatedClass.isAnnotationPresent( jakarta.persistence.Table.class );
if ( hasTableAnnotation ) {
final jakarta.persistence.Table tableAnnotation = annotatedClass.getAnnotation( jakarta.persistence.Table.class );
table = tableAnnotation.name();
schema = tableAnnotation.schema();
@ -769,15 +770,11 @@ public class EntityBinder {
createTable( inheritanceState, superEntity, schema, table, catalog, uniqueConstraints );
}
else {
// must be a SINGLE_TABLE mapping for a subclass
if ( !table.isEmpty() ) {
final Table superTable = persistentClass.getRootClass().getTable();
if ( !logicalTableName( table, schema, catalog )
.equals( superTable.getQualifiedTableName() ) ) {
throw new AnnotationException( "Entity '" + annotatedClass.getName()
+ "' is a subclass in a 'SINGLE_TABLE' hierarchy and may not be annotated '@Table'"
+ " (the root class declares the table mapping for the hierarchy)");
}
// if we get here we have SINGLE_TABLE inheritance
if ( hasTableAnnotation ) {
throw new AnnotationException( "Entity '" + annotatedClass.getName()
+ "' is a subclass in a 'SINGLE_TABLE' hierarchy and may not be annotated '@Table'"
+ " (the root class declares the table mapping for the hierarchy)");
}
// we at least need to properly set up the EntityTableXref
bindTableForDiscriminatedSubclass( superEntity.getEntityName() );

View File

@ -8,19 +8,36 @@ import jakarta.persistence.OrderColumn;
import jakarta.persistence.Table;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.JiraKey;
import org.hibernate.testing.orm.junit.ServiceRegistry;
import org.hibernate.testing.orm.junit.ServiceRegistryScope;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.Test;
import java.util.List;
import org.hibernate.AnnotationException;
import org.hibernate.boot.MetadataSources;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.fail;
@SuppressWarnings("JUnitMalformedDeclaration")
@JiraKey("HHH-18288")
@SessionFactory
@DomainModel(annotatedClasses = {SubclassIndexTest.Foo.class, SubclassIndexTest.Bar.class})
public class SubclassIndexTest {
@Test void test(SessionFactoryScope scope) {
scope.getSessionFactory();
@Test
@ServiceRegistry
void test(ServiceRegistryScope registryScope) {
try {
new MetadataSources( registryScope.getRegistry() )
.addAnnotatedClasses( Foo.class, Bar.class )
.buildMetadata();
fail( "Expecting exception" );
}
catch (AnnotationException expected) {
assertThat( expected.getMessage() ).contains( "is a subclass in a 'SINGLE_TABLE' hierarchy and may not be annotated '@Table'" );
}
}
@Entity