繁体   English   中英

JPA 条件生成器仅在连接列不包含特定值时返回实体

[英]JPA criteria builder only return entity if join column does not contain a certain value

我有两个班级:

@Entity
@Table(name = "foo")
data class Foo {
  @Id
  val id: UUID
  // a lot more values

  @ElementCollection(fetch = FetchType.EAGER)
  @CollectionTable(name = "foo_bar", joinColumns = [JoinColumn(name = "foo_bar_id")])
  val bars: Set<FooBar>?
}

data class FooBar {
  val fooBarId: UUID
  val bar: Bar
  val validUntil: Instant
}

目前,我使用 Predicate 和 CriteriaBuilder 对此实体进行了广泛搜索 function。 我希望能够在我们的搜索中仅包含 Foo 对象,这些对象在其 FooBar JoinColumn 行中具有特定的 Bar 值,而 function 仅在没有任何joinColumn 行具有该特定 Bar 值的情况下返回 FooBar Object。

我现在所拥有的是:



    fun searchPaged(
        // numerous criteria values
        flipFilterOnBar: Boolean?,
        bar: Bar?,
        pageable: Pageable
    ): Page<Foo> {
  return findAll({ root, _, cb ->
    
    val predicates = mutableListOf<Predicate>(
                cb.or(
                   //some criteria
                )
            )

     //more criteria
     //
     //

      if (bar != null) {
                val joinColumn: Join<Foo, FooBar> = root.join<Foo?, FooBar?>("bars", JoinType.INNER)
                if (flipFilterOnBar == true) { //Nullable boolean
                    
                    // this part makes the query return a row for each join column and filters out only the filtered value (returning more rows than if this clauses was not added.
                    predicates.add(
                    cb.notEqual(joinColumn.get<Bar>("bar"), bar)
                    )
                } else {
                    predicates.add(
                        cb.equal(joinColumn.get<Bar>("bar"), bar)
                    )
                }
            }

            cb.and(*predicates.toTypedArray())
        }, pageable)

我想知道如果没有 FooBar 行包含指定的 Bar 值,我如何使用此处的条件构建器仅返回 Bar 实体。 我现在编写它的方式将所有 Foo 和 FooBar 行连接在一起,并且只过滤具有指定 Bar 值的组合行,返回不是我想要的其他组合行。

例如,如果我有一个包含 3 个 FooBar 的 Foo,我想过滤掉所有具有值为“b”的 Bar 的 Foo。 查询检索此 Foo 3 次(每个 Bar 元素一次),然后仅过滤其中包含值为“b”的 Foo 的组合行。 返回其他两行。 我希望查询不返回任何这些行,因为其中一行包含值“b”。

尝试使用类似于所有通过先前谓词的 foos 的谓词,并且其 id 不在 foos 的 id 集合中,foobars 的 bar 值为“b”。

        Subquery<UUID> subquery = criteriaQuery.subquery(UUID.class);
        Root<Foo> fooSubqueryRoot = subquery.from(Foo.class);
        Join<Foo, FooBar> fooBarSubqueryJoin = fooSubqueryRoot.join("bars");
        Join<FooBar, Bar> barSubqueryJoin = fooBarSubqueryJoin.join("bar");
        subquery.where(cb.equal(barSubqueryJoin.get("value"), "b"));
        //foos ids of foos with foobars whose bar value is "b"
        subquery.distinct(true).select(fooSubqueryRoot.get("id"));
        //exclude foos with foobars whose bar value is "b"
        predicates.add(root.get("id").in(subquery).not());

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM