Hibernate Search - 索引多对一关系
Hibernate Search - indexed ManyToOne relation
我有两个索引实体
@Entity @Indexed
public class AEntity implements {
@Id
private String externalId;
@OneToMany(cascade = CascadeType.ALL) @JoinColumn(name = "externalId")
private Set<BEntity> bEntities;
}
@Entity @Indexed
public class BEntity {
@JsonIgnore @ManyToOne
@IndexedEmbedded(depth = 1)
@JoinColumn(name = "externalId", updatable = false)
private AEntity aEntity;
}
现在,当我这样搜索 BEntity 时:
private QueryBuilder getQuery() {
FullTextEntityManager fullTextEntityManager = getFullTextEntityManager();
return fullTextEntityManager.getSearchFactory().buildQueryBuilder().forEntity(BEntity.class).get();
}
Query query = getQuery().bool()
.must(getQuery().keyword().onField("aEntity.externalId").matching(externalId).createQuery())
.createQuery();
我有以下错误:
Unable to find field aEntity.externalId in BEntity
默认情况下 @IndexedEmbedded
不包含 ID 字段。
您可以对文档 ID 字段执行的操作有限制,因此我个人建议定义一个与文档 ID 分开的实际字段。这样你就可以安全地使其可排序、规范化或任何你想要的:
@Entity @Indexed
public class AEntity implements {
@Id
@DocumentId(name = "docId") // CHANGE HERE
@Field // CHANGE HERE
private String externalId;
@OneToMany(cascade = CascadeType.ALL) @JoinColumn(name = "externalId")
private Set<BEntity> bEntities;
}
@Entity @Indexed
public class BEntity {
@JsonIgnore @ManyToOne
@IndexedEmbedded(depth = 1)
@JoinColumn(name = "externalId", updatable = false)
private AEntity aEntity;
}
但是如果你真的想使用文档ID字段,你可以使用@IndexedEmbedded(includeEmbeddedObjectId = true)
:
@Entity @Indexed
public class AEntity implements {
@Id
private String externalId;
@OneToMany(cascade = CascadeType.ALL) @JoinColumn(name = "externalId")
private Set<BEntity> bEntities;
}
@Entity @Indexed
public class BEntity {
@JsonIgnore @ManyToOne
@IndexedEmbedded(depth = 1, includeEmbeddedObjectId = true) // CHANGE HERE
@JoinColumn(name = "externalId", updatable = false)
private AEntity aEntity;
}
不要忘记在更改注释后重新索引数据。
我有两个索引实体
@Entity @Indexed
public class AEntity implements {
@Id
private String externalId;
@OneToMany(cascade = CascadeType.ALL) @JoinColumn(name = "externalId")
private Set<BEntity> bEntities;
}
@Entity @Indexed
public class BEntity {
@JsonIgnore @ManyToOne
@IndexedEmbedded(depth = 1)
@JoinColumn(name = "externalId", updatable = false)
private AEntity aEntity;
}
现在,当我这样搜索 BEntity 时:
private QueryBuilder getQuery() {
FullTextEntityManager fullTextEntityManager = getFullTextEntityManager();
return fullTextEntityManager.getSearchFactory().buildQueryBuilder().forEntity(BEntity.class).get();
}
Query query = getQuery().bool()
.must(getQuery().keyword().onField("aEntity.externalId").matching(externalId).createQuery())
.createQuery();
我有以下错误:
Unable to find field aEntity.externalId in BEntity
默认情况下 @IndexedEmbedded
不包含 ID 字段。
您可以对文档 ID 字段执行的操作有限制,因此我个人建议定义一个与文档 ID 分开的实际字段。这样你就可以安全地使其可排序、规范化或任何你想要的:
@Entity @Indexed
public class AEntity implements {
@Id
@DocumentId(name = "docId") // CHANGE HERE
@Field // CHANGE HERE
private String externalId;
@OneToMany(cascade = CascadeType.ALL) @JoinColumn(name = "externalId")
private Set<BEntity> bEntities;
}
@Entity @Indexed
public class BEntity {
@JsonIgnore @ManyToOne
@IndexedEmbedded(depth = 1)
@JoinColumn(name = "externalId", updatable = false)
private AEntity aEntity;
}
但是如果你真的想使用文档ID字段,你可以使用@IndexedEmbedded(includeEmbeddedObjectId = true)
:
@Entity @Indexed
public class AEntity implements {
@Id
private String externalId;
@OneToMany(cascade = CascadeType.ALL) @JoinColumn(name = "externalId")
private Set<BEntity> bEntities;
}
@Entity @Indexed
public class BEntity {
@JsonIgnore @ManyToOne
@IndexedEmbedded(depth = 1, includeEmbeddedObjectId = true) // CHANGE HERE
@JoinColumn(name = "externalId", updatable = false)
private AEntity aEntity;
}
不要忘记在更改注释后重新索引数据。