Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parameter not recognized by Spring Data ElasticsearchCrudRepository

I have a @Document "Project" class with the following variables:

    @Document(indexName = "project-index")
    public class Project {
    ...
        @JsonProperty("import_id")
        private Long importId;

        private String title;
   ...
    //getters and setters
    }

My JSON is like the following

    {
       "_id" : ObjectId("5269fd92e4b0c74e42976c91"),
    ...
        "import_id" : NumberLong(1),
         "title" : "Something",
    ...
    }

And my @Repository class is like:

    @Repository
    public interface ProjectRepository extends ElasticsearchCrudRepository<Project, String>{
        Page<Project> findByImportId(Long importId, Pageable page);
        Page<Project> findByTitleLike(String title, Pageable page);
    }

The problem is that findByImportId is not getting any data, while findByTitleLike is working fine and letting me fetch data, I tried with different configurations, but seems that SpringData is not resolving that the field that has to search by import_id, even when it has @JsonProperty configured. Moreover, I tried to change the variable name to import_id (also the getter and setter) but in such case, when I tried to use findByImport_Id, findByImport_id, findByImport__Id or findByImport__id get an error at runtime.

Any idea how can I map a db variable with an underscore with a java variable that later could be recognized by ElasticsearchCrudRepository?

Thanks

like image 234
Daniel Oliva Avatar asked Nov 10 '22 02:11

Daniel Oliva


1 Answers

from the Spring Docs:

If your property names contain underscores (e.g. first_name) you can escape the underscore in the method name with a second underscore. For a first_name property the query method would have to be named findByFirst__name(…).

So it seems the correct would be findByImport__id

like image 91
airtonjal Avatar answered Nov 15 '22 09:11

airtonjal