【问题标题】:Hibernate Search: Store List of Children and retrieve via projectionHibernate Search:存储子列表并通过投影检索
【发布时间】:2017-07-05 21:39:07
【问题描述】:

我有以下 2 个带有休眠和休眠搜索注释的类:

父类:

@Indexed
@Entity
public class Parent {
    @Id
    @Column(name="parent_id")
    private Long id;

    @Field(store = Store.YES)
    private String name;

    @IndexedEmbedded
    @OneToMany(fetch = FetchType.LAZY, mappedBy = "parent", targetEntity = Child.class)
    private List<Child> childList;

    //getters and setters
}

儿童班:

@Entity
public class Child {
    @Id
    private Long id;

    @ManyToOne(targetEntity = Parent.class)
    @ContainedIn
    @JoinColumn(name = "parent_id")
    private Parent parent;

    @Field(store = Store.YES)
    private String name;
}

我为上述场景创建了一个索引。现在我正在尝试通过搜索给定的父名称来获取所有子名称。

我在场地上使用投影如下:

Query searchQuery = queryBuilder.keyword().onField("name").matching("test").createQuery();
FullTextQuery fullTextQuery = fullTextEntityManager.createFullTextQuery(searchQuery, Parent.class);
fullTextQuery.setProjection("childList.name");

现在,当我尝试运行查询以检索搜索结果时,我得到了父模型的唯一第一个子模型的名称。

当我使用 Luke 查看索引时,我可以看到文档中的所有值。

如何获取索引中存储的所有子名称的列表?

【问题讨论】:

标签: java hibernate lucene hibernate-search


【解决方案1】:

在此示例中,您有一个需要“转身”的模型。

您的目标是查询列表子名称,因此您需要搜索Child 实体。

您仍然可以在每个孩子中包含“父母的姓名”,并限制您的查询。

@Entity @Indexed
public class Child {
...


Query searchQuery = queryBuilder.keyword().onField("parent.name").matching("test").createQuery();
FullTextQuery fullTextQuery = fullTextEntityManager.createFullTextQuery(searchQuery, Child.class);
fullTextQuery.setProjection("name");

【讨论】:

    猜你喜欢
    • 2021-08-26
    • 2022-07-18
    • 1970-01-01
    • 2012-08-04
    • 1970-01-01
    • 1970-01-01
    • 2015-10-09
    • 1970-01-01
    • 2011-05-25
    相关资源
    最近更新 更多