简体   繁体   中英

JPA/Hibernate Criteria API JOIN

I have a problem with join in CriteriaAPI.
I want to get the result with unique Forma entities, but currently I get multiple entities (their number is equal to the number of Gniazdo entities).

This is my code:

    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Forma> query = cb.createQuery(Forma.class);
    Root<Forma> root = query.from(Forma.class);

    Join<Forma, Gniazdo> socketJoin = root.join("gniazda", JoinType.INNER);

    List<Predicate> predicates = new ArrayList<Predicate>();

    predicates.add(root.get("dataUsuniecia").isNull());
    predicates.add(socketJoin.get("dataUsuniecia").isNull());

    Predicate[] conditions = predicates.toArray(new Predicate[predicates.size()]);
    query.where(cb.and(conditions));

    List<Forma> queryResult = em.createQuery(query).getResultList();

Please help.

您需要使用query.distinct(true)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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