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

#1

I have a Request entity which has a collection of Stages and each stage has a collection of StageIitems. 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):

stages.stageItems.aDetail:blah

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

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:

@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;
    //...
}
#2

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

Let’s continue the discussion there, if necessary.

closed #3