Search code examples
javaspringspring-bootlucenehibernate-search

Hibernate search does not create index after some methods


I have application with two objects, TableDoc and TableRow.

@Entity
@Indexed
public class TableDoc{
  @OneToMany(mappedBy = "tableDoc", fetch = FetchType.LAZY)
  @IndexedEmbedded
  @JsonManagedReference
  private Set<TableRow> tableRows = new HashSet<>();
}
@Entity
@Indexed
public class TableRow{
  @ManyToOne
  @JoinColumn(name = "table_id")
  @JsonBackReference
  private TableDoc tableDoc;

  @FullTextField
  @Column(name = "cell_values")
  private String cellValues;
}

I am creating and appending new TableRow into TableDoc by this simplified method:

  public TableRow create(TableRow tableRow) {
    return tableRowRepository.save(tableRow);
  }

Everything is persisted correctly in database. For some reason, Hibernate search 6 with lucene does not index/reindex tableRow.cellValues when I create TableRow with create method above.

I have to reindex whole application to be able to query by cellValues values. Also update does create index, only create is problematic. This is my update method:

  public TableRow update(String id, TableRow tableRow) {
    TableRow tableRowToUpdate = tableRowRepository.findById(id)
        .orElseThrow(() -> new ObjectNotFoundException("TableRow", "id", id));
    tableRowToUpdate.setCellValues(tableRow.getCellValues());
    return tableRowRepository.save(tableRowToUpdate);
  }

Both, update and create method do receive same object:

{
 "tableDoc":{"id":"myLongTableDocId"},
 "cellValues":"one"
}

Solution

  • As @yrodiere commented and provided documentation link, it is needed to update both sides of a relationship.

    It is now working with create method like this:

      public TableRow create(TableRow tableRow) {
        TableDoc tableDoc = tableDocRepository.findById(tableRow.getTableDoc().getId())
            .orElseThrow(() -> new ObjectNotFoundException();
        tableRow.setTableDoc(tableDoc);
        return tableRowRepository.save(tableRow);
      }