Hibernate搜索只添加IndexedEmbedded类中的DocumentId。

问题描述 投票:0回答:1

我有一个实体 "Invoice",这个实体和Customer-Entity有很多对一的关系。这个Customer-Entity也是从其他Entity用于Hibernate Search的,所以有很多Hibernate Search的注解。对于Invoice HS-Index,我只想在Invoice索引中使用Customer.id,而不使用Customer的其他属性,这怎么可能,因为在文档中我没有找到任何关于它的具体说明。

hibernate-search
1个回答
1
投票

在最近的版本的Hibernate Search中,你只需要使用 @IndexedEmbedded(includePaths = "id").

不过Hibernate Search 3.4已经非常老了(9年了),而且缺少了很多功能。我建议你升级,因为你很有可能会遇到在这个版本中永远无法解决的bug。

如果你真的要坚持使用3.4,我相信你唯一的解决办法就是写一个自定义桥接。

public class CustomerIdBridge implements StringBridge {

    public String objectToString(Object object) {
        Customer customer = (Customer) object;
        if ( customer == null ) {
           return null;
        }
        Object id = customer.getId();
        return id == null ? null : id.toString();
    }
}

然后像这样应用桥接

@ManyToOne(...)
@Field(bridge = @FieldBridge(impl = CustomerIdBridge.class))
private Customer customer;                

结果字段将被简单地命名为 "customer"(与你的属性同名)。

请看 此处 关于Hibernate Search 3.4.2中桥的更多信息。

© www.soinside.com 2019 - 2024. All rights reserved.