Home > Net >  java.lang.reflect.InaccessibleObjectException: Unable to make field private final transient java.net
java.lang.reflect.InaccessibleObjectException: Unable to make field private final transient java.net

Time:10-02

I'm trying to delete elastic documents in bulk amount, but I get exception "java.lang.reflect.InaccessibleObjectException: Unable to make field private final transient java.net.InetSocketAddress" when elasticsearchOperations.delete(...) is executed. Here is the code, not able to figure out what is causing it. Any help is much appreciated.

private void deleteElasticDocuments(String catalogId) {
    List<String> docIdList = new ArrayList<>(250);
    String queryText = martServerContext.getQueryCacheInstance().getQuery(QUERY_PORTAL_GET_OBJECTS_IN_PORTAL_BY_MODEL);
    MapSqlParameterSource mapSqlParameterSource = new MapSqlParameterSource();
    mapSqlParameterSource.addValue("cId", Integer.parseInt(catalogId));
    namedParameterJdbcTemplate.query(queryText, mapSqlParameterSource, (resultSet -> {
        int objectId = resultSet.getInt(O_ID);
        String docId = catalogId   objectId;
        docIdList.add(docId);
        if (docIdList.size() == 250) {
            Query query = new NativeSearchQueryBuilder()
                    .withQuery(QueryBuilders.idsQuery().addIds(docIdList.toArray(String[]::new)))
                    .build();
            elasticsearchOperations.delete(query, IndexCoordinates.of("portal_idx"));
            docIdList.clear();
        }
        // elasticsearchOperations.delete(docId, IndexCoordinates.of("portal_idx"));
    }));
    if (docIdList.size() < 250) {
        Query query = new NativeSearchQueryBuilder()
                .withQuery(QueryBuilders.idsQuery().addIds(docIdList.toArray(String[]::new)))
                .build();
        elasticsearchOperations.delete(query, IndexCoordinates.of("portal_idx"));
        docIdList.clear();
    }
}

CodePudding user response:

Along with query and index, I added my Class MetadataSearch.class and now it is working.

elasticsearchOperations.delete(query, MetadataSearch.class, IndexCoordinates.of("portal_idx"));

Edit (by P.J.Meisch):

Let me explain why you got this error. You were calling the function with the signature

ElasticsearchOperations.delete(Object, IndexCoordinates)

Here ElasticsearchOperations takes the first parameter as an entity object which should be deleted and tries to find its id-property. Normally you'd pass in an instance of MetadataSearch in your case. Spring Data Elasticsearch already would know what properties this object has from the initial setup.

But now it gets an object that is a NativeSearchQuery and which is not known as an entity type and so Spring Data Elasticsearch analyses the object and its properties and this goes down to properties of the properties ...

NativeSearchQuery has a property of type RequestBuilder, this has a SearchRequest which in turn contains a TransportAddress and this has an InetSocketAddress. And here the inspection fails with the error you see.

So indeed using the delete variant taking a query, the entity class and the IndexCoordinates is the right way. If your MetadataSearch class is annotated with @Document(indexName="portal_idx") you can omit the last parameter.

  • Related