在@OneToMany关联中无法进行休眠搜索排序?

时间:2017-11-17 13:11:28

标签: sorting indexing one-to-many hibernate-search

我尝试按订购日期为客户排序项目列表。日期只能通过Item。 orderPositions.order.orderDate 来获取。但是 @InededEmbedded 不起作用。没有执行或错误,但结果只按HS逻辑排序。

    @Entity
    @Indexed
    public class Item{
        @Id
        private long id;

        @Field(index = Index.YES, store = Store.YES, analyse = Analyse.YES, analyser = @Analyzer(definition = Constant.ANALYSER))
        private String description;

        @OneToMany(mappedBy = "item")
        @IndexedEmbedded
        private List<OrderPosition> orderPositions;

        @ManyToOne
        @IndexedEmbedded
        private Company company;           
    //getter&setter
    }

    @Entity
    public class OrderPosition{
        @Id
        private long id;

        @ManyToOne
        private Item item;

        @ManyToOne
        @IndexedEmbedded
        private Order order;
    //getter&setter
    }

    @Entity
    public class Order{
        @Id
        private long id;

        @ManyToOne
        private Customer customer;

        @Field(index = Index.NO, store = Store.NO, analyze = Analyze.NO)
        @SortableField
        private String orderDate;
    //getter&setter
    }

    @Entity
    public class Company{
        @Id
        private long id;

        @Field(index = Index.NO, store = Store.NO, analyze = Analyze.NO)
        @SortableField
        private String name;
    //getter&setter
    }

如果我按项目排序列表。 company.name 它可以正常工作。

    queryService.buildFullTextQuery("searchText", Item.class, "description", "company.name").getResultList();

如果我按项目对List进行排序。 orderPosition.order.orderDate 默认排序(HS-logic)

    queryService.buildFullTextQuery("searchText", Item.class, "description", "orderPositions.order.orderDate").getResultList();

我以这种方式构建FullTextQuery:

    public FullTextQuery buildFullTextQuery(@NonNull String searchText, @NonNull Class<?> clazz, @NonNull String... fields) throws Exception {
        FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(getEntityManager());
        QueryBuilder qb = fullTextEntityManager.getSearchFactory().buildQueryBuilder().forEntity(clazz).get();
        Query query = qb.keyword().onField(fields[0]).matching(searchText).createQuery();
        SortField sortField = new SortField(fields[1], SortField.Type.STRING, false);
        Sort sort = new Sort(sortField);
        return fullTextEntityManager.createFullTextQuery(query, clazz).setSort(sort);
    }

我认为HS无法找到@OneToMany的关联。有没有办法解决这个问题?

提前谢谢

0 个答案:

没有答案