Criteria JPA 2 with 3 tables

后端 未结 3 1838
既然无缘
既然无缘 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:48

    For three tables involved.

    CriteriaBuilder builder = theEntityManager.getCriteriaBuilder(); CriteriaQuery query1 = builder.createQuery(BasicMemberInfo.class);

        Root<Table1> table1 = query1.from(Table1.class); 
        Root<Table2> table2 = query1.from(Table2.class);
        Root<Table3> table3 = query1.from(Table3.class);
    
       List<Predicate> conditions = new ArrayList();
        conditions.add(builder.equal(table3.get("Table1").get("memberId"), table1.get("memberId")));
        conditions.add(builder.equal(table2.get("tableid").get("memberId"), table1.get("memberId")));
        conditions.add(builder.equal(table2.get("indicator"), 'Y'));
        conditions.add(builder.equal(table3.get("StatusCd"), "YES"));
    
        TypedQuery<BasicCustInfo> typedQuery = theEntityManager.createQuery(
                query1.multiselect(table1.get("memberId"), table2.get("AcctId"))
                .where(conditions.toArray(new Predicate[] {}))
        );
    
        List<BasicMemberInfo> custList = typedQuery.getResultList();
    

    public class BasicMemberInfo {

    String memberId;
    String AcctId;
    
    public BasicCustInfo() {
        // TODO Auto-generated constructor stub
    }
    
    public BasicMemberInfo( BigDecimal memberId,String AcctId ) {
        this.memberId = memberId;
        this.AcctId = AcctId;
    }
    
    public BigDecimal getmemberId() {
        return memberId;
    }
    public void setmemberId(BigDecimal memberId) {
        memberId = memberId;
    }
    public String getAcctId() {
        return AcctId;
    }
    public void setAcctId(String AcctId) {
        AcctId = AcctId;
    }
    

    }

    0 讨论(0)
  • 2020-12-07 23:57

    Checkout this test with even more than three tables . Also use static meta model instead of using direct attribute names.

        @Test
    @Rollback(false)
    @Transactional
    public void   
    fetch() {
        CriteriaBuilder cb = 
    entityManager.getCriteriaBuilder();
        CriteriaQuery<Instructor> cq = 
    cb.createQuery(Instructor.class);
        Root<Instructor> root = 
    cq.from(Instructor.class);
        root.join(Instructor_.idProof);
        root.join(Instructor_.vehicles);
        Join<Instructor, Student> insStuJoin = 
    root.join(Instructor_.students);
        insStuJoin.join(Student_.instructors);
        Join<Student, Vehicle> stuVehcileJoin.   
    = insStuJoin.join(Student_.vehicles);
        Join<Vehicle, Document> 
    vehicleDocumentJoin = 
    stuVehcileJoin.join(Vehicle_.documents);
    
    DataPrinters.
    listDataPrinter.accept.   
    (queryExecutor.fetchListForCriteriaQuery
          (cq.select(root).where
    (cb.greaterThan(root.get(Instructor_.id), 2),                        
               cb.in(vehicleDocumentJoin.get
                              (Document_.name)).value("1")
    .value("2").value("3")));
    }
    
    0 讨论(0)
  • 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<Update>, and creates something which is behind the scenes also a Path<Detail>. From that, you can build other joins. Try this (code not tested):

    Root<Update> fromUpdates = query.from(Update.class);
    Join<Update, Detail> details = fromUpdates.join("details");
    Join<Detail, Associate> associate = details.join("associate");
    List<Predicate> conditions = new ArrayList();
    conditions.add(builder.equal(associate.get("associateId"), associateId));
    conditions.add(builder.isNull(details.get("ack_date")));
    
    TypedQuery<Update> typedQuery = em.createQuery(query
            .select(fromUpdates)
            .where(conditions.toArray(new Predicate[] {}))
            .orderBy(builder.asc(fromUpdates.get("updateId")))
            .distinct(true)
    );
    
    0 讨论(0)
提交回复
热议问题