Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Spring Data Mongodb Query by embedded document id #3624

Closed
viniciusgati opened this issue Apr 11, 2021 · 2 comments
Closed

Spring Data Mongodb Query by embedded document id #3624

viniciusgati opened this issue Apr 11, 2021 · 2 comments
Labels
status: superseded An issue that has been superseded by another

Comments

@viniciusgati
Copy link

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.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Apr 11, 2021
@christophstrobl
Copy link
Member

Thanks for reporting. The MongoDB module operates up @DBRef which is an @Reference but special to the store.
By using @Reference instead of @DBRef the Branch entity will not be linked, but embedded into the TaskGroups object. We're already working to enhance the support for linking (#3602) but for now please switch to @DBRef which should solve the issue.

@christophstrobl christophstrobl added status: waiting-for-feedback We need additional information before we can continue and removed status: waiting-for-triage An issue we've not yet triaged labels Apr 12, 2021
@viniciusgati
Copy link
Author

Hi thanks for your help, sadly i cant use dbref, i am migrating from it due to performance reasons.
But thanks anyway i will keep the workaround.

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Apr 12, 2021
@mp911de mp911de added status: superseded An issue that has been superseded by another and removed status: feedback-provided Feedback has been provided labels May 18, 2021
@mp911de mp911de closed this as completed May 18, 2021
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
status: superseded An issue that has been superseded by another
Projects
None yet
Development

No branches or pull requests

4 participants