From f947927234485e64d35d20a8217a67bc49a7a95d Mon Sep 17 00:00:00 2001 From: Gavin King Date: Fri, 21 Jun 2024 10:12:35 +0200 Subject: [PATCH] HHH-18288 @Index specified by subclass in SINGLE_TABLE inheritance I'm definitely not a fan of @Table annotations on subclasses in SINGLE_TABLE hierarchies. However, unfortunately we've already decided to tolerate it. Signed-off-by: Gavin King --- .../boot/model/internal/EntityBinder.java | 5 +-- .../orm/test/schema/SubclassIndexTest.java | 42 +++++++++++++++++++ 2 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/schema/SubclassIndexTest.java diff --git a/hibernate-core/src/main/java/org/hibernate/boot/model/internal/EntityBinder.java b/hibernate-core/src/main/java/org/hibernate/boot/model/internal/EntityBinder.java index d9b0ffac32..766e14ecd4 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/model/internal/EntityBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/model/internal/EntityBinder.java @@ -750,8 +750,7 @@ public class EntityBinder { final String table; final String catalog; final UniqueConstraint[] uniqueConstraints; - boolean hasTableAnnotation = annotatedClass.isAnnotationPresent( jakarta.persistence.Table.class ); - if ( hasTableAnnotation ) { + if ( annotatedClass.isAnnotationPresent( jakarta.persistence.Table.class ) ) { final jakarta.persistence.Table tableAnnotation = annotatedClass.getAnnotation( jakarta.persistence.Table.class ); table = tableAnnotation.name(); schema = tableAnnotation.schema(); @@ -771,7 +770,7 @@ public class EntityBinder { } else { // must be a SINGLE_TABLE mapping for a subclass - if ( hasTableAnnotation ) { + if ( !table.isEmpty() ) { final Table superTable = persistentClass.getRootClass().getTable(); if ( !logicalTableName( table, schema, catalog ) .equals( superTable.getQualifiedTableName() ) ) { diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/schema/SubclassIndexTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/schema/SubclassIndexTest.java new file mode 100644 index 0000000000..31a235dd43 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/schema/SubclassIndexTest.java @@ -0,0 +1,42 @@ +package org.hibernate.orm.test.schema; + +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import jakarta.persistence.Index; +import jakarta.persistence.OneToMany; +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.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.Test; + +import java.util.List; + +@JiraKey("HHH-18288") +@SessionFactory +@DomainModel(annotatedClasses = {SubclassIndexTest.Foo.class, SubclassIndexTest.Bar.class}) +public class SubclassIndexTest { + + @Test void test(SessionFactoryScope scope) { + scope.getSessionFactory(); + } + + @Entity + @Table(name = "FOO") + static class Foo { + @Id + long id; + } + + @Entity + @Table(indexes = @Index(name="IDX", columnList = "text")) + static class Bar extends Foo { + @OneToMany + @OrderColumn + List foo; + + String text; + } +}