updating an index on an subselect entity

101 Views Asked by At

In my project, I have entities that I cannot change, but I need to create an index on top one of them. That an example what I want to archive:

That is the entities that I cannot change:

@Entity
@Table(name = "main")
public class FirstEntity {

    @Id
    private Long id;

    @ManyToOne
    private SecondEntity secondaryEntity;
}

@Entity
@Table(name = "secondary")
public class SecondEntity {

    @Id
    private Long id;
    
    private String name;

}

My entity

@Indexed
@Entity
@Subselect("select * from main")
//@Table(name = "main")
//@Immutable
public class ThirdEntity {
    @Id
    private Long id;

    @ManyToOne
    private SecondEntity secondaryEntity;

    @Transient
    @GenericField(sortable = Sortable.YES)
    @IndexingDependency(derivedFrom = {
            @ObjectPath(@PropertyValue(propertyName = "secondaryEntity"))
    })
    public String getName() {
        return secondaryEntity.getName();
    }
}

Whenever the SecondEntity changes, the index for ThirdEntity remains the same.

I wrote a small/simple example https://github.com/YaroslavTir/reindex-subselect-entity

I have in mind a workaround solution. Using hibernateListeners and rebuild index for ThirdEntity by hand, but that is not really nice solution.

1

There are 1 best solutions below

2
yrodiere On

Your indexing dependency annotation is incomplete, it should be something like:

   @IndexingDependency(derivedFrom = {
        @ObjectPath(
            @PropertyValue(propertyName = "secondaryEntity"),
            @PropertyValue(propertyName = "name")
        )
    })