0

I am trying to iterate query result of Querydsl. For iterating query result I am using for each loop.But I am getting class cast exception.

I want nBuildId for finding building name which is in building table. So how I can iterate this List<Tuple> for getting column.

I tried like this

 public  List<Tuple> loadUnclamiedRoomGrid(Integer nBuildId, String sFloor) {

        QRoom room = QRoom.room;        
        QRoomDepartmentMapping roomDepartmentMapping = QRoomDepartmentMapping.roomDepartmentMapping;

        JPAQuery<Tuple> query = new JPAQuery<Tuple>(em);

        query
             .from(room) 
             .where(room.nRoomId.notIn
                         (JPAExpressions.select(roomDepartmentMapping.nRoomId)
                               .from(roomDepartmentMapping)
                         )
               );

        if (nBuildId != null && nBuildId !=0) {
            query.where(room.nBuildId.eq(nBuildId));
        }

        if(sFloor != null) {
            query.where(room.sFloor.eq(sFloor));
        }   

        List<Tuple> queryResult = query.fetch();

        for(Tuple row : queryResult) {

            System.out.println("Build Id " +row.get(room.nBuildId));
        }

    return queryResult; 

    }

Error

message: "com.spacestudy.model.Room cannot be cast to com.querydsl.core.Tuple",
Exception: "java.lang.ClassCastException" 
4
  • What is your query which gets executed? Can you share the entire method containing this code Commented Aug 31, 2018 at 11:15
  • I added Query code Commented Aug 31, 2018 at 11:30
  • Does changing List<Tuple> queryResult = query.fetch(); to List<Room> queryResult = query.fetch(); fix the exception? Commented Aug 31, 2018 at 12:09
  • its not taking List<Room> getting error as Type mismatch: cannot convert from List<Tuple> to List<Room> Commented Aug 31, 2018 at 12:16

1 Answer 1

1
 public  List<Room> loadUnclamiedRoomGrid(Integer nBuildId, String sFloor) {

        QRoom room = QRoom.room;        
        QRoomDepartmentMapping roomDepartmentMapping = QRoomDepartmentMapping.roomDepartmentMapping;

        JPAQuery<Room> query = new JPAQuery<Room>(em);

        query
             .from(room) 
             .where(room.nRoomId.notIn
                         (JPAExpressions.select(roomDepartmentMapping.nRoomId)
                               .from(roomDepartmentMapping)
                         )
               );

        if (nBuildId != null && nBuildId !=0) {
            query.where(room.nBuildId.eq(nBuildId));
        }

        if(sFloor != null) {
            query.where(room.sFloor.eq(sFloor));
        }   

        List<Room> queryResult = query.fetch();

        for(Room row : queryResult) {

            System.out.println("Build Id " + room.nBuildId);
        }

    return queryResult; 

    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.