问题
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