'CriteriaBulder in recursive related DB

I have a table with a recursive relationship and I'm trying to find all records that matched the "name" field at 2 levels: root and parent:

@Entity
public class NvCoinCategoryDict implements Persistence {

    @Id
    @Column(name = "id")
    private Long id;

    @JoinColumn(name = "category_id")
    @ManyToOne
    private NvCategory category;
}


@Entity
public class NvCategory implements Persistence {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    @Column(name = "name")
    private String name;

    @JoinColumn(name = "parent_category_id")
    @ManyToOne
    private NvCategory parent;
}

For this I create two predicates

 (root, criteriaQuery, builder) -> {
                        final ListJoin<CatCoin, NvCoinCategoryDict> categoryDicts = root.joinList("categoryDicts", JoinType.LEFT);
                        final Predicate inParent = categoryDicts.get("category").get("parent").get("name").in(filter.getCategory());
                        final Predicate inRoot = categoryDicts.get("category").get("name").in(filter.getCategory());
                        return builder.or(inParent, inRoot);  

Each predicate on its own handles the search as it should, but in the OR pair, the search occurs only at the parent level. Why is this happening and can it be fixed?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source