我正在尝试迭代Querydsl的查询结果。为了迭代查询结果,我在每个循环中使用。但是我遇到了类强制转换异常。
我想要nBuildId来查找建筑物表中的建筑物名称。因此,我如何迭代此List<Tuple>
以获得专栏。
我尝试过这样
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;
}
错误
message: "com.spacestudy.model.Room cannot be cast to com.querydsl.core.Tuple",
Exception: "java.lang.ClassCastException"
答案 0 :(得分: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;
}