Hibernate Criteria Query to get specific columns

前端 未结 5 1589
情深已故
情深已故 2020-11-29 20:43

I am using Criteria Query in my code. It always fires select * from ...

Instead I want to neglect one column(field) from my query as that field have lar

5条回答
  •  离开以前
    2020-11-29 20:52

    You can use JPQL as well as JPA Criteria API for any kind of DTO projection(Mapping only selected columns to a DTO class) . Look at below code snippets showing how to selectively select various columns instead of selecting all columns . These example also show how to select various columns from joining multiple columns . I hope this helps .

    JPQL code :

    String dtoProjection = "new com.katariasoft.technologies.jpaHibernate.college.data.dto.InstructorDto"
                    + "(i.id, i.name, i.fatherName, i.address, id.proofNo, "
                    + " v.vehicleNumber, v.vechicleType, s.name, s.fatherName, "
                    + " si.name, sv.vehicleNumber , svd.name) ";
    
            List instructors = queryExecutor.fetchListForJpqlQuery(
                    "select " + dtoProjection + " from Instructor i " + " join i.idProof id " + " join i.vehicles v "
                            + " join i.students s " + " join s.instructors si " + " join s.vehicles sv "
                            + " join sv.documents svd " + " where i.id > :id and svd.name in (:names) "
                            + " order by i.id , id.proofNo , v.vehicleNumber , si.name , sv.vehicleNumber , svd.name ",
                    CollectionUtils.mapOf("id", 2, "names", Arrays.asList("1", "2")), InstructorDto.class);
    
            if (Objects.nonNull(instructors))
                instructors.forEach(i -> i.setName("Latest Update"));
    
            DataPrinters.listDataPrinter.accept(instructors);
    

    JPA Criteria API code :

    @Test
        public void fetchFullDataWithCriteria() {
            CriteriaBuilder cb = criteriaUtils.criteriaBuilder();
            CriteriaQuery cq = cb.createQuery(InstructorDto.class);
    
            // prepare from expressions
            Root root = cq.from(Instructor.class);
            Join insIdProofJoin = root.join(Instructor_.idProof);
            Join insVehicleJoin = root.join(Instructor_.vehicles);
            Join insStudentJoin = root.join(Instructor_.students);
            Join studentInsJoin = insStudentJoin.join(Student_.instructors);
            Join studentVehicleJoin = insStudentJoin.join(Student_.vehicles);
            Join vehicleDocumentJoin = studentVehicleJoin.join(Vehicle_.documents);
    
            // prepare select expressions.
            CompoundSelection selection = cb.construct(InstructorDto.class, root.get(Instructor_.id),
                    root.get(Instructor_.name), root.get(Instructor_.fatherName), root.get(Instructor_.address),
                    insIdProofJoin.get(IdProof_.proofNo), insVehicleJoin.get(Vehicle_.vehicleNumber),
                    insVehicleJoin.get(Vehicle_.vechicleType), insStudentJoin.get(Student_.name),
                    insStudentJoin.get(Student_.fatherName), studentInsJoin.get(Instructor_.name),
                    studentVehicleJoin.get(Vehicle_.vehicleNumber), vehicleDocumentJoin.get(Document_.name));
    
            // prepare where expressions.
            Predicate instructorIdGreaterThan = cb.greaterThan(root.get(Instructor_.id), 2);
            Predicate documentNameIn = cb.in(vehicleDocumentJoin.get(Document_.name)).value("1").value("2");
            Predicate where = cb.and(instructorIdGreaterThan, documentNameIn);
    
            // prepare orderBy expressions.
            List orderBy = Arrays.asList(cb.asc(root.get(Instructor_.id)),
                    cb.asc(insIdProofJoin.get(IdProof_.proofNo)), cb.asc(insVehicleJoin.get(Vehicle_.vehicleNumber)),
                    cb.asc(studentInsJoin.get(Instructor_.name)), cb.asc(studentVehicleJoin.get(Vehicle_.vehicleNumber)),
                    cb.asc(vehicleDocumentJoin.get(Document_.name)));
    
            // prepare query
            cq.select(selection).where(where).orderBy(orderBy);
            DataPrinters.listDataPrinter.accept(queryExecutor.fetchListForCriteriaQuery(cq));
    
        }
    

提交回复
热议问题