Java 如果字段不在db表中,如何在Hibernate搜索中为布尔@字段编制索引

Java 如果字段不在db表中,如何在Hibernate搜索中为布尔@字段编制索引,java,hibernate-search,Java,Hibernate Search,我在Hibernate搜索中对布尔@字段进行索引时遇到了一个问题,问题是当对象已更改时,其余字段也会更改,只有布尔字段保留对象的旧状态 @JsonIgnore @Field(name = "isWarning", index = Index.YES) @SortableField(forField = "isWarning") private boolean isWarning() { //some logic } 解决这个问题的正确方法是什么 我假设您提到的这个“逻辑”可以访问其他实体

我在Hibernate搜索中对布尔@字段进行索引时遇到了一个问题,问题是当对象已更改时,其余字段也会更改,只有布尔字段保留对象的旧状态

@JsonIgnore
@Field(name = "isWarning", index = Index.YES)
@SortableField(forField = "isWarning")
private boolean isWarning() {
   //some logic
}
解决这个问题的正确方法是什么

我假设您提到的这个“逻辑”可以访问其他实体。您需要使用
isWarning
方法告诉Hibernate Search这些实体包含在实体中

假设
isWarning
方法是在一个名为
maintentity
的实体中定义的,它从另一个名为
SomeOtherEntity
的实体访问数据

SomeOtherEntity
中,您将拥有关联的反面:

public class SomeOtherEntity {
  @ManyToOne // Or @OneToOne, or whatever
  private MainEntity mainEntity;
}
只需添加@ContainedIn,您就应该很好:

public class SomeOtherEntity {
  @ManyToOne // Or @OneToOne, or whatever
  @ContainedIn
  private MainEntity mainEntity;
}
请注意,不幸的是,如果频繁更新
SomeOtherEntity
,这可能会对性能产生重大影响:Hibernate搜索无法准确知道
SomeOtherEntity
中使用了
SomeOtherEntity
的哪个部分,因此,即使
SomeOtherEntity
中的更改不会影响
isWarning
的结果,每次
SomeOtherEntity
更改时都会重新索引
main entity
。A,但它仍然挂起。

我假设您提到的这个“逻辑”访问其他实体。您需要使用
isWarning
方法告诉Hibernate Search这些实体包含在实体中

假设
isWarning
方法是在一个名为
maintentity
的实体中定义的,它从另一个名为
SomeOtherEntity
的实体访问数据

SomeOtherEntity
中,您将拥有关联的反面:

public class SomeOtherEntity {
  @ManyToOne // Or @OneToOne, or whatever
  private MainEntity mainEntity;
}
只需添加@ContainedIn,您就应该很好:

public class SomeOtherEntity {
  @ManyToOne // Or @OneToOne, or whatever
  @ContainedIn
  private MainEntity mainEntity;
}

请注意,不幸的是,如果频繁更新
SomeOtherEntity
,这可能会对性能产生重大影响:Hibernate搜索无法准确知道
SomeOtherEntity
中使用了
SomeOtherEntity
的哪个部分,因此,即使
SomeOtherEntity
中的更改不会影响
isWarning
的结果,每次
SomeOtherEntity
更改时都会重新索引
main entity
。A,但它仍然挂起。

我们已经尝试了此解决方案,但到目前为止不起作用:),唯一可行的方法是,但我们不想使用它。是通过在保存后强制hibernate重新编制obj的索引。我已经使用过很多次了,而且效果很好,所以您可能需要进行调查。您使用的是哪个版本的Hibernate搜索?也许您受到了影响?我们已经尝试过此解决方案,但到目前为止还不起作用:),唯一可行的方法是,但我们不想使用它。是通过在保存后强制hibernate重新编制obj的索引。我已经使用过很多次了,而且效果很好,所以您可能需要进行调查。您使用的是哪个版本的Hibernate搜索?也许你受到了什么影响?