Query-Annotation in Spring-Data Mongo to find map key / value

I have an object containing a key / value-Map that I want to use with Spring-Data Mongo. Perseverance worked very easily. Now I want to implement a very dynamic query on this map, where the KEY of the query (or part of it) must also be parameterized:

public interface EntityItemRepository extends MongoRepository<EntityItem, String>{

@Query("{values.?0:?1}")
public List<EntityItem> findByValues(String key,String value);

....

      

so that I can easily accomplish

findByValues('lastname','Schneider');

      

Here is my essence:

public class EntityItem {

@Id
private String id;

private Map<String, Object> values = new HashMap<String, Object>();

...

      

I debugged StringBasedMongoQuery and found out that only the value (so "? 1") is bound, but not the key ("? 0") of the query. I made some changes to StringBasedMongoQuery and now it works ... but I'm wondering if there is a better solution than hacking my own StringBasedMongoQuery :)

In org.springframework.data.mongodb.repository.query.StringBasedMongoQuery I changed (# 262):

    for (String field : dbo.keySet()) {
        collectParameterReferencesIntoBindings(bindings, dbo.get(field));
    }

      

to

    for (String field : dbo.keySet()) {
        collectParameterReferencesIntoBindings(bindings, field);
        collectParameterReferencesIntoBindings(bindings, dbo.get(field));
    }

      

and added some nasty quote line replacements ... as I said it works now, but it doesn't.

Hopefully this is not a duplicate, I searched for the watch and couldn't find any problems.

+3


source to share





All Articles