Skip to content
Advertisement

Spring Data Mongodb Query by embedded document id

i am trying to query a model that contains a @Reference as attribute like:

public class TaskGroups {

    @Reference
    Branch branch;
 
.
.
.

}

and so on, then i have a query with more logic and code but in resume i have to filter by the field _id from Branch object, its just id on the object but on database its _id, ok no problem its normal behavior for the framework.

ok now the code that did not work:

public class TaskGroupRepositoryImpl implements TaskGroupCustomRepository {

    private final MongoTemplate mongoTemplate;

    @Autowired
    public TaskGroupRepositoryImpl(MongoTemplate mongoTemplate) {
        this.mongoTemplate = mongoTemplate;
    }

    @Override
    public List<TaskGroup> query(TaskGroupDynamicQuery queryObject) {
        Query query = new Query();
        final List<Criteria> criterias = new ArrayList<>();

        criterias.add(Criteria.where("deleted").is(false));

        if (queryObject.getBranchId() != null) {
            criterias.add(Criteria.where("branch._id").is(new ObjectId(queryObject.getBranchId())));
        }

.
.
.
}

So when the code runs the query changes the field “branch._id” to only branch! and then no records returned…

With this behavior i needed to make a workaround, mapping the result to just Object and then converting to the taskGroup class , then the query has no modifications on the attribute names and the filter ocurs without problems.

Is this a bug? or its normal behavior to? is there any workaround to avoid this attribute name change when id fields?

Att.

Advertisement

Answer

For every on stuck with this problem to, actually there is no solution to this, on the official repository the maintainers talked with me and this is frozen the way it is, to solve it i will need to use @DbRef, but for performance reasons most of the time this is not a viable solution.

They said that in the future it will be a possibility because they are working on the enhancement of the api but for today there is no way to do it.

the enhancement issue is: enhancement issue

the base issue for consult: base issue

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement