Criteria JPA 2 with 3 tables

后端 未结 3 1904
既然无缘
既然无缘 2020-12-07 23:22

I\'m trying to create a criteria to retrieve some objects from 3 tables (Associate, Update and Detail). A Detail has reference to Associate and Update, and an Update has ref

3条回答
  •  时光取名叫无心
    2020-12-07 23:58

    Each join takes you from the leftish type parameter to the rightish one. So, the details join of my code (second line) starts from fromUpdates, that is a Path, and creates something which is behind the scenes also a Path. From that, you can build other joins. Try this (code not tested):

    Root fromUpdates = query.from(Update.class);
    Join details = fromUpdates.join("details");
    Join associate = details.join("associate");
    List conditions = new ArrayList();
    conditions.add(builder.equal(associate.get("associateId"), associateId));
    conditions.add(builder.isNull(details.get("ack_date")));
    
    TypedQuery typedQuery = em.createQuery(query
            .select(fromUpdates)
            .where(conditions.toArray(new Predicate[] {}))
            .orderBy(builder.asc(fromUpdates.get("updateId")))
            .distinct(true)
    );
    

提交回复
热议问题