Search code examples
javahibernatehibernate-onetomany

hibernate - building a query across @OneToMany


I've got 2 classes:

@Entity
public class Basic {
    private String a;
    @OneToMany
    private List<Element> elements;
}

@Entity
public class Element {
    private String b;
}

what I'm trying to do is create a Predicate that tests for if a Basic has an Element where b is some value (say b.equals("hello world")). I'm constrained to implement this method:

public Predicate testBasic(String elementValue, Root<Basic> root, CriteriaQuery<?> query, CriteriaBuilder cb);

thank you very much!


Solution

  • I know HQL usually has access to submembers torugh the dot operator.

    Have you tried:

    where basics.elements.b LIKE %xxx%
    

    Else the solution is to use a JOIN

    from basics b right join elements e on e.fk_b = b.id where e.b like %str%