I have a problem using elasticsearch with hibernate search 6. Let's assume we have this setup :
@Entity
@Table(name = "entityA")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@Indexed(index = "entityA")
public class EntityA {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
@SequenceGenerator(name = "sequenceGenerator")
@GenericField
private Long id;
@Column(name = "name")
@KeywordField
private String name;
@OneToOne
@JoinColumn(unique = true)
@Cascade(value = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.SAVE_UPDATE})
@IndexedEmbedded
@IndexingDependency(reindexOnUpdate = ReindexOnUpdate.SHALLOW)
private EntityB entityB;
}
@Entity
@Table(name = "entityB")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class EntityB {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
@SequenceGenerator(name = "sequenceGenerator")
@GenericField
private Long id;
@Column(name = "name")
@KeywordField
private String name;
@OneToOne(cascade = {}, fetch = FetchType.EAGER, targetEntity = EntityA.class)
@JoinColumn(name = "id", nullable = false)
@IndexingDependency(reindexOnUpdate = ReindexOnUpdate.DEFAULT)
private EntityA entityA
}
When I first persist EntityA, that being the entity that is indexed, the EntityB is persisted in the elasticsearch index as a child of EntityA. This is ok. The problem appears when I directly edit EntityB and make changes to it, this changes are not propagated to the elasticsearch index. Is something that i am missing?
UPDATE 1
After @yrodiere answers, i made this changes :
@OneToOne
@JoinColumn(unique = true)
@Cascade(value = {CascadeType.MERGE, CascadeType.PERSIST,
CascadeType.SAVE_UPDATE})
@IndexedEmbedded
@AssociationInverseSide(inversePath = @ObjectPath(
@PropertyValue(
propertyName = "entitya" ) ))
private EntityB entityB;
The problem still persist. If i do something like this :
EntityB b = entityBRepository.findById(5051L).get();
b.setProperty("3333");
entityBRepository.save(b);
Regards.
You explicitly instructed Hibernate Search to behave exactly that way:
reindexOnUpdate = ReindexOnUpdate.SHALLOWmeans "reindexEntityAwhen theentityBproperty ofEntityAchanges, but not when a property ofEntityBitself (e.g. its name) changes".See this section of the reference documentation.
I'm guessing you added that to get rid of an exception telling you that Hibernate Search was unable to find the inverse side of the association
EntityA.entityB. In your case, it seems you should rather tell Hibernate Search what the inverse side of that association is. Either add amappedByto one side of the association (Warning: this will change your DB schema), or use@AssociationInverseSide(see this section of the documentation).