简体   繁体   中英

PSQLException: ERROR: operator does not exist: bigint = text[]

Looking resolution in postgres DB and MySQL DB working fine

I/P: String ordIds = "343,21,343";

Map fetchDataSource(String orgIds){

    ResultSet resultSet = null;
    PreparedStatement statement = null;
    String str[] = orgIds.split(",");
    List<Integer> orgIdList = new ArrayList<Integer>();

    Map<String, String> dataSourceMap = new LinkedHashMap<String, String>();
    orgIdList = Arrays.asList(str).stream().map(Integer::valueOf).collect(Collectors.toList());
    String query = "select ds.ds_path,string_agg(org.organization_id::text, ',') as org_id from c_organization org join org_ds_detail ds on org.org_ds_detail_id = ds.org_ds_detail_id where org.organization_id in (?) GROUP BY ds.ds_path";
    //String queryIN = orgIdList.stream().map(orgId -> String.valueOf(orgId)).collect(Collectors.joining(",", "(", ")"));

    try {
        Connection connection = DBConnection.getInstance("CLINICS_GLOBAL");
        statement = connection.prepareStatement(query);
        **Array orgIdsInArray = connection.createArrayOf("text",orgIdList.toArray());
        statement.setArray(1, orgIdsInArray);**
        logger.info("Executing Query:" + query);
        resultSet = statement.executeQuery();
        while (resultSet.next()) {
            dataSourceMap.put(resultSet.getString(1), resultSet.getString(2));
        }

    } catch (SQLException e) {
        logger.info("Exception:" + e);
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            statement.close();
            resultSet.close();
            DBConnection.closeConnection();
        } catch (SQLException e) {
            logger.info("Exception while closing connection:" + e.getMessage());
            e.printStackTrace();
        }
    }
    return dataSourceMap;
}

Exception (Logs):

13:16:16,217 INFO  [DataSourceConnection] (default task-2) Exception:org.postgresql.util.PSQLException: ERROR: operator does not exist: bigint = text[]
  Hint: No operator matches the given name and argument type(s). You might need to add explicit type casts.
  Position: 190
13:16:16,219 ERROR [stderr] (default task-2) org.postgresql.util.PSQLException: ERROR: operator does not exist: bigint = text[]
13:16:16,219 ERROR [stderr] (default task-2)   Hint: No operator matches the given name and argument type(s). You might need to add explicit type casts.
13:16:16,220 ERROR [stderr] (default task-2)   Position: 190
13:16:16,221 ERROR [stderr] (default task-2)    at 

Well, as the error message says, you are passing a text[] array and trying to compare that with a bigint value. Would you expect the Long.valueOf(1) == new String[] {"1", "2"} to work in Java?

You need to create a bigint array:

Array orgIdsInArray = connection.createArrayOf("bigint",orgIdList.toArray());

And you need to change your operator that compares the column with the array:

where org.organization_id = any (?)

Another option that doesn't involve creating an java.sql.Array would be to convert the comma separated list to an array in SQL, rather than in Java. For that change your SQL statement to:

where org.organization_id = any (string_to_array(?, ',')::bigint[])

And then simply pass the original string to the PreparedStatement:

statement.setString(1, orgIds);

Then you can get rid of converting the String to an array and then to a List in Java.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM