내 코드는 현재 진행됩니다h2에 MySQL의 WHERE IN IN과 유사한 쿼리/절이 있습니까? 다음과 같이
public List<DeviceOrganizationMetadataHolder> getChildrenByParentId(List<String> parentIds) throws DeviceOrganizationDAOException {
List<DeviceOrganizationMetadataHolder> children = new ArrayList<>();
Connection conn;
PreparedStatement stmt = null;
ResultSet rs = null;
DeviceOrganizationMetadataHolder deviceMetadataHolder;
String[] data = parentIds.toArray(new String[parentIds.size()]);
try {
conn = this.getConnection();
String sql = "SELECT * FROM DEVICE_ORGANIZATION_MAP WHERE DEVICE_PARENT IN (?)";
stmt = conn.prepareStatement(sql);
data = parentIds.toArray(data);
stmt.setObject(1, data);
rs = stmt.executeQuery();
while (rs.next()) {
deviceMetadataHolder = this.loadOrganization(rs);
children.add(deviceMetadataHolder);
}
} catch (SQLException e) {
throw new DeviceOrganizationDAOException("Error occurred for device list with while retrieving children.", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
return children;
}
}
그러나 나는 parentIds와 배열을 전달하려고 단위 테스트에서, null가 돌려 남아있다하더라도. 내가이에서 측정 할 수 다음 중 하나입니다
- 배열 데이터가 점점되지는 제대로 때문에 출력이 null로오고,를 참조하십시오.
- h2 또는 에 의해 지원되지 않는 경우 그 대신에 다른 구현이 필요합니다.
어디서 잘못 되었나요?
편집 - 비슷한 duplicate question에 태그가 지정되었습니다. StringBuilder와 루프를 사용하는 것이 좋겠지 만 쿼리 자체를 사용하여 더 명확한 방식으로 수행 할 수있는 답변을 찾고있었습니다. 즉, 목록 대신 배열로 매개 변수를 설정
[IN 절에 매개 변수 목록이있는 PreparedStatement] 가능한 복제본 (https://stackoverflow.com/questions/3107044/preparedstatement-with-list-of-parameters-in-a-in-clause) –