hibernatehibernate-searchhibernate-validatorhibernate-toolshibernate-reactive

Hibernate Search does not update indexed embedded


I am using Postgres and Hibernate Search 7.1 in my project. But, I am facing a issue with a IndexedEmbedded in my entity. Basically, I have 2 entities and a 1-N relationship between them. The following example shows how it is mapped:

@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
@Indexed
public class EntityA {
    @Id
    private Long id;
    
    ...

    @ManyToOne
    @JoinColumn(name = "entity_b_id")
    @IndexedEmbedded(includePaths = {"name"})
    private EntityB entityb;

    ...
}

@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
@Indexed
public class EntityB {
    @Id
    private Long id;

    private String name;
    
    ...

    @OneToMany(mappedBy = "entityb", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @IndexedEmbedded
    private Collection<EntityA> entitya;

    ...
}

The problem here is when I try to update the EntityA and set a new EntityB, this update is done on Postgres where I can see the field entity_b_id with the new id about the EntityB, but inside the Elasticsearch the EntityB keep showing a reference to the old EntityA.

Example: EntityA with id 1 has a relationhip with EntityB where id is 5, when I search inside Elasticsearch about the EntityA with id 1 I am able to see the EntityB with id 5 as it has a @IndexedEmbedded, also if I search on Elasticsearch by the EntityB with id 5 I am able to see the EntityA with id 1. But, when I try to update the EntityA and set a new EntityB to this relationship, this update is done on Postgres but it is not being reflected on Elasticsearch in the EntityB index, so when I search on Elasticsearch about the EntityA I can see the new relationship, but when I search on EntityB it keep showing the old relationship.

It is happening only in the update process, when I try to insert or delete I can see the update in both sides.

Does anyone have any idea what this problem could be?

I tried to use a cascade in both sides and also I tried to call the EntityB to do a update, but did not work.


Solution

  • It's likely that you've encountered a problem described here: asymmetric association updates

    Make sure that you update both sides of the association when making this kind of an update.