Spring with Querydsl :java.lang.ClassCastException

北慕城南 提交于 2020-02-06 07:56:52

问题


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" 

回答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; 

    }


来源:https://stackoverflow.com/questions/52114040/spring-with-querydsl-java-lang-classcastexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!