Spring JPA规范OR子句返回空列表

时间:2019-02-25 11:05:42

标签: java spring spring-boot spring-data-jpa specifications

我正在尝试在我的项目中实现这一点:

        Specification<Delivery> shipmentSpec = ((root, query, builder) ->
                builder.or(builder.equal(root.join("entries").get("shipment").get("senderGeography"), geo),
                           builder.equal(root.join("entries").get("shipment").get("receiverGeography"), geo)));

        Specification<Delivery> collectSpec = ((root, query, builder) ->
               builder.equal(root.join("entries").get("collect").get("geography"), geo));

        repository.findAll(Specification.where(shipmentSpec).or(collectSpec);

它不返回任何东西。但如果我仅使用一种规格,例如repository.findAll(Specification.where(shipmentSpec));

,效果很好

repository.findAll(Specification.where(collectSpec));

投放实体的字段为:

@OneToMany(mappedBy = "delivery")
private List<Entry> entries = new ArrayList<>();

Entry实体具有字段:

@ManyToOne
@JoinColumn(name = "SHIPMENT_ID")
private Shipment shipment;

@ManyToOne
@JoinColumn(name = "COLLECT_ID")
private Collect collect;

这些字段之一始终为null,但不能同时为空。

运送和收集实体具有字段:

@ManyToOne(optional = false)
@JoinColumn(name = "GEOGRAPHY_ID", nullable = false)
private Geography geography;

(货件有2个地理位置)

我需要一个选项来返回所有在条目中具有装运或按指定地理位置收集的交货实体。

谢谢。

已更新。

我尝试使用@Query获取实体,但其行为与规范方法相同,返回空列表:

@Query("select d from Delivery d, Entry e where e.delivery.id = d.id " +
        "and (e.shipment.senderGeography = ?1 or e.shipment.receiverGeography = ?1 or e.collect.geography = ?1)")
List<Delivery> findByGeography(Geography geo);

仅当我在查询中留下(e.shipment.senderGeography = ?1 or e.shipment.receiverGeography = ?1)e.collect.geography = ?1子句时,它才有效。 但不是两个。

0 个答案:

没有答案