# Creating an index in subtype using JPA

**URL:** <https://discourse.hibernate.org/t/creating-an-index-in-subtype-using-jpa/10425>\
**Category:** Hibernate ORM\
**Created:** [October 9, 2024, 7:16am UTC](https://discourse.hibernate.org/t/creating-an-index-in-subtype-using-jpa/10425 "2024-10-09T07:16:11Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![art.kobelev](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/art.kobelev/32/3383_2.png) [@art.kobelev](https://discourse.hibernate.org/u/art.kobelev)\
**Post date:** [October 9, 2024, 7:16am UTC](https://discourse.hibernate.org/t/creating-an-index-in-subtype-using-jpa/10425/1 "2024-10-09T07:16:11Z")

</div>

Hi!

Can you help me, please? I’m trying to get rid of outdated hibernate annotations but having trouble with creating the index.

Is it possible to create index for subtype column ‘field2’ via JPA annotations? I can do it with deprecated org.hibernate.annotations.Index, but i don’t understand how to do it with jakarta.persistance.

Thanks!

```auto
@Entity
@Inheritance
@Table(name = "tbl_class",
        indexes = {
            @Index(name = "idx_field1", columnList = "field1")
})
class ClassA
{
    @Id
    @GeneratedValue
    private Long id;

    @Column
    private String field1;

    ...
}

@Entity
class ClassB extends ClassA
{ 
    /* @org.hibernate.annotations.Index(name="idx_field2") */
    @Column
    private String field2;

   ...
}

```

---

<div class="post-metadata">

**Author:** ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)\
**Post date:** [October 9, 2024, 11:32am UTC](https://discourse.hibernate.org/t/creating-an-index-in-subtype-using-jpa/10425/2 "2024-10-09T11:32:39Z")

</div>

Theoretically you could do this by repeating the `@Table` annotation:

```java
@Entity
@Inheritance
@Table(name = "tbl_class",
        indexes = {
            @Index(name = "idx_field1", columnList = "field1")
})
class ClassA { ... }

@Entity
@Table(indexes = {
            @Index(name = "idx_field2", columnList = "field2")
})
class ClassB extends ClassA { ... }

```

But there is an [ongoing discussion](https://github.com/jakartaee/persistence/issues/644) about whether this should be allowed and what is the right way to do this.

---

<div class="post-metadata">

**Author:** ![art.kobelev](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/art.kobelev/32/3383_2.png) [@art.kobelev](https://discourse.hibernate.org/u/art.kobelev)\
**Post date:** [October 9, 2024, 12:20pm UTC](https://discourse.hibernate.org/t/creating-an-index-in-subtype-using-jpa/10425/3 "2024-10-09T12:20:12Z")

</div>

Thanks for the answer. I tried to do this, but unfortunately it doesn’t work.

It seems that the deprecated annotation should exist until a solution appears for JPA.

---

<div class="post-metadata">

**Author:** ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)\
**Post date:** [October 10, 2024, 8:17am UTC](https://discourse.hibernate.org/t/creating-an-index-in-subtype-using-jpa/10425/4 "2024-10-10T08:17:27Z")

</div>

Something that definitely works is to specify the index on the table of the root class e.g.

```java
@Entity
@Inheritance
@Table(name = "tbl_class",
        indexes = {
            @Index(name = "idx_field1", columnList = "field1"),
            @Index(name = "idx_field2", columnList = "field2")
})
class ClassA { ... }

@Entity
class ClassB extends ClassA { ... }

```

I agree though, that it feels a bit odd since the field/column is only introduced in the subclass.

---

<div class="post-metadata">

**Author:** ![art.kobelev](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/art.kobelev/32/3383_2.png) [@art.kobelev](https://discourse.hibernate.org/u/art.kobelev)\
**Post date:** [October 10, 2024, 12:40pm UTC](https://discourse.hibernate.org/t/creating-an-index-in-subtype-using-jpa/10425/5 "2024-10-10T12:40:39Z")

</div>

It works! But that’s really weird.

I hope that JPA will be improved someday.

Thanks for your help!

---

<div class="post-metadata">

**Author:** ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)\
**Post date:** [October 16, 2024, 5:15pm UTC](https://discourse.hibernate.org/t/creating-an-index-in-subtype-using-jpa/10425/6 "2024-10-16T17:15:12Z")

</div>

FTR, the Hibernate ORM team discussed this topic today, and we concluded that defining the `@Table` annotation on single table inheritance subclasses is wrong. The solution to use is to declare all indexes and other objects on the root class of the inheritance hierarchy:

```java
@Entity
@Inheritance
@Table(name = "tbl_class",
        indexes = {
            @Index(name = "idx_field1", columnList = "field1"),
            @Index(name = "idx_field2", columnList = "field2")
})
class ClassA { ... }

@Entity
class ClassB extends ClassA { ... }

```

---

<div class="post-metadata">

**Author:** ![Peter\_Vansan](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/peter_vansan/32/4204_2.png) [@Peter\_Vansan](https://discourse.hibernate.org/u/Peter_Vansan)\
**Post date:** [December 2, 2025, 8:50am UTC](https://discourse.hibernate.org/t/creating-an-index-in-subtype-using-jpa/10425/7 "2025-12-02T08:50:41Z")

</div>

This was a bad decision. What if you don’t know the index when ClassA is defined (ie, it’s a library)? ???

---

<div class="post-metadata">

**Author:** ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)\
**Post date:** [December 2, 2025, 11:52am UTC](https://discourse.hibernate.org/t/creating-an-index-in-subtype-using-jpa/10425/8 "2025-12-02T11:52:57Z")

</div>

You are free to raise your concerns on the Jakarta persistence ticket and try to convince others about your use case: [clarify "primary table" in inheritance mappings · Issue #652 · jakartaee/persistence · GitHub](https://github.com/jakartaee/persistence/issues/652)

---

<div class="post-metadata">

**Author:** ![Peter\_Vansan](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/peter_vansan/32/4204_2.png) [@Peter\_Vansan](https://discourse.hibernate.org/u/Peter_Vansan)\
**Post date:** [December 2, 2025, 11:58am UTC](https://discourse.hibernate.org/t/creating-an-index-in-subtype-using-jpa/10425/9 "2025-12-02T11:58:17Z")

</div>

> <https://github.com/jakartaee/persistence/issues/652#issuecomment-3601672632>
>
> In #644 we saw that inheritance mappings are somewhat underspecified, opening up… the potential for unexpected (and unnecessary)lack of portability between implementations, and that the term "primary table" is left undefined. We should nail this stuff down.

Done
