# Hibernate search / lucene - Cannot search by field in subclass in collection

**URL:** <https://discourse.hibernate.org/t/hibernate-search-lucene-cannot-search-by-field-in-subclass-in-collection/2703>\
**Category:** Hibernate Search\
**Created:** [May 12, 2019, 3:51pm UTC](https://discourse.hibernate.org/t/hibernate-search-lucene-cannot-search-by-field-in-subclass-in-collection/2703 "2019-05-12T15:51:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![burebista](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@burebista](https://discourse.hibernate.org/u/burebista)\
**Post date:** [May 12, 2019, 3:51pm UTC](https://discourse.hibernate.org/t/hibernate-search-lucene-cannot-search-by-field-in-subclass-in-collection/2703/1 "2019-05-12T15:51:44Z")

</div>

I have a `Request` entity which has a collection of `Stage`s and each stage has a collection of `StageIitem`s. Stage items can be of several types and I use in JPA/Hibernate `SINGLE_TABLE` as inheritance strategy with a discriminator column.

I need my query to return requests that have particular detail in a stage item of type A (field `aDetail` in class `StageItemA`). I cannot get the lucene query to see the field `aDetail` in `StageItemA`.

So this lucene query does not work (returns 0 results):

```auto
stages.stageItems.aDetail:blah

```

But searching based on fields in the base class `StageItem` works:

```auto
stages.stageItems.comment:yuppie

```

Using Luke, I can the fields from `StageItem`, like `stages.stageItems.comment`, but no field from `StageItemA`, like `stages.stageItems.aDetail`.

Entities:

```auto
@Entity
@Table(name = "REQUEST")
@Indexed(index = "RequestIndex")
class Request implements Serializable {
    //...
    @OneToMany(fetch = FetchType.EAGER, cascade = {CascadeType.ALL})
    @MapKeyColumn(name = "type", length = 50, nullable = false)
    @JoinTable(
            name = "REQUEST_STAGE",
            joinColumns = @JoinColumn(name = "REQUEST_ID", referencedColumnName = "id"),
            inverseJoinColumns = @JoinColumn(name = "STAGE_ID", referencedColumnName = "id")
    )
    @IndexedEmbedded
    private Map<String, Stage> stages = new HashMap<>();
    //...
}

@Entity
@Table(name = "STAGE")
public class Stage implements Serializable {
    //...
    @OneToMany(fetch = FetchType.EAGER, targetEntity = StageItem.class, cascade = {
            CascadeType.ALL,
    }, orphanRemoval = true)
    @JoinTable(
            name = "STAGE_TO_STAGE_ITEM",
            joinColumns = @JoinColumn(name = "STAGE_ID"),
            inverseJoinColumns = @JoinColumn(name = "STAGE_ITEM_ID")
    )
    @Fetch(FetchMode.JOIN)
    @IndexedEmbedded
    private Set<StageItem> stageItems = new HashSet<>();
    //...
}

@Entity
@Table(name = "STAGE_ITEM")
@DiscriminatorColumn(name = "TYPE", discriminatorType = DiscriminatorType.STRING)
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public abstract class StageItem implements Serializable {
    //...
    @Column(name = "TYPE")
    @Field
    protected String type;

    @Column(name="COMMENT")
    @Field
    protected String comment;
    //...
}

@Entity
@DiscriminatorValue(value = "A")
public class StageItemA extends StageItem {
    //...
    @Column(name="A_DETAIL")
    @Field
    private String aDetail;
    //...
}

```

---

<div class="post-metadata">

**Author:** ![yrodiere](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/yrodiere/32/1760_2.png) [@yrodiere](https://discourse.hibernate.org/u/yrodiere)\
**Post date:** [May 13, 2019, 7:05am UTC](https://discourse.hibernate.org/t/hibernate-search-lucene-cannot-search-by-field-in-subclass-in-collection/2703/2 "2019-05-13T07:05:57Z")

</div>

This was cross-posted from stack overflow. I answered there: [https://stackoverflow.com/a/56107249/6692043](https://stackoverflow.com/a/56107249/6692043)

Let’s continue the discussion there, if necessary.

---

<div class="post-metadata">

**Author:** ![yrodiere](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/yrodiere/32/1760_2.png) [@yrodiere](https://discourse.hibernate.org/u/yrodiere)\
**Post date:** [May 13, 2019, 7:06am UTC](https://discourse.hibernate.org/t/hibernate-search-lucene-cannot-search-by-field-in-subclass-in-collection/2703/3 "2019-05-13T07:06:02Z")

</div>

Closed on May 13, 2019, 7:06am UTC


