Search code examples
lucenehibernate-searchsynchronize

Synchronize two lucene documents mapped for the same table (entity)


Consider two identical Java entities (PersonM1, PersonM2) mapped for the same table (PERSON) with the same attributes defined as:

    @Entity
    @Table(name = "PERSON")
    @Indexed
    public class PersonM1 {

    @Id
    @DocumentId
    private long id;

    @Field
    @Column
    private String name;
    //setters, gettes, ...

}

@Entity
@Table(name = "PERSON")
@Indexed
public class PersonM2 {

    @Id
    @DocumentId
    private long id;

    @Field
    @Column
    private String name;
    //setters, gettes, ...
}

Is there a way to update PersonM2 indexes when we update a PersonM1 object? If The object PersonM1 is updated, changes are persisted on the database, but not in PersonM2 index directory, so PersonM2 indexes won't be correct in this case.

Shall I do it manually (update PersonM1 when PersonM2 is updated)?

Note: Java inheritance trick is not relevant!


Solution

  • There is no way currently, as the identity of the indexed type is represented directly the the class instance of the model. This will change in Hibernate Search 5, so in that version you might have a "clean" solution for such a scenario but I don't know yet if we will expose an API for this, and how this would look like. You'll probably have to provide your custom implementation of "entity identity".