Criteria JPA 2 with 3 tables

后端 未结 3 1901
既然无缘
既然无缘 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: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 cq = 
    cb.createQuery(Instructor.class);
        Root root = 
    cq.from(Instructor.class);
        root.join(Instructor_.idProof);
        root.join(Instructor_.vehicles);
        Join insStuJoin = 
    root.join(Instructor_.students);
        insStuJoin.join(Student_.instructors);
        Join stuVehcileJoin.   
    = insStuJoin.join(Student_.vehicles);
        Join 
    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")));
    }
    

提交回复
热议问题