(Hibernate-Search-lucene)如何索引另一个对象(B)中的字段,其主键在对象A中作为外键存在

anhgbhbe  于 2023-08-05  发布在  Lucene
关注(0)|答案(1)|浏览(232)

我有两个班:

A类:(待检索)

@Entity
@Indexed
Class A {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(updatable = false, nullable = false)
    protected Long id;

    @JsonIgnore
    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "b_id", referencedColumnName = "id")
    protected B b; // foreign key
}

字符串

B类:

@Entity
Class B {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(updatable = false)
    @JsonIgnore
    private Long id;

    private String fieldToBeIndexed; // ** I want to index this field **

}


因为我正在做一个项目,搜索只针对A类:
例如:FullTextQuery fullTextQuery = fullTextEntityManager.createFullTextQuery(query,A);
我想通过在搜索框中传递B的字段(fieldToBeIndexed)的值来搜索A。我如何才能做到这一点。我如何索引B的字段(fieldToBeIndexed),以便可以根据B的字段(fieldToBeIndexed)搜索A。

bqujaahr

bqujaahr1#

您可以使用@IndexEmbedded来执行此操作:

@Entity
@Indexed
Class A {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(updatable = false, nullable = false)
    protected Long id;

    @JsonIgnore
    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "b_id", referencedColumnName = "id")
    @IndexedEmbedded
    protected B b; // foreign key
}

字符串
我假设您使用的是Hibernate Search 5,因此这里有指向该版本IndexedEmbedded文档的链接https://docs.jboss.org/hibernate/search/5.11/reference/en-US/html_single/#search-mapping-associated

相关问题