Spring Data Mongo Property Expression Query not returning any results

161 views Asked by At

I have recently started working on a Spring Boot project and chose MongoDB as my database. I have the following Document structure.

Following is the User document structure.

@Data
@Builder(setterPrefix = "with")
@Document(collection = "users")
@JsonIgnoreProperties(value = {"password", "createdAt", "updatedAt"}, allowSetters = true)
public class User {

    @Id
    @Indexed
    private String id;

    @Indexed(unique = true, direction = IndexDirection.DESCENDING)
    private String username;

    private String name;

    private String password;

    @CreatedDate
    private Date createdAt;

    @LastModifiedDate
    private Date updatedAt;
}

I have the following structure for Borrow document.

@Document(collection = "borrows")
@Data
@SuperBuilder(setterPrefix = "with")
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
@AllArgsConstructor
@NoArgsConstructor
public abstract class Borrow {

    @Id
    @EqualsAndHashCode.Include
    private String id;

    @DocumentReference
    private User borrower;

    @DocumentReference
    private User borowee;

    private Date expectedReturnDate;

    private Date actualReturnDate;

    private String place;

    private String occasion;

    private BorrowStatus status;

    public abstract String getType();
}

and two sub classes for Borrow as follows.

BorrowMoney

@Document(collection = "borrows")
@Data
@EqualsAndHashCode(callSuper = true, onlyExplicitlyIncluded = true)
@SuperBuilder(setterPrefix = "with")
@BsonDiscriminator(key = "type", value = "Money")
public class BorrowMoney extends Borrow{

    private Double amount;

    @Override
    public String getType() {
        return "Money";
    }
}
@Document(collection = "borrows")
@Data
@SuperBuilder(setterPrefix = "with")
@EqualsAndHashCode(callSuper = true, onlyExplicitlyIncluded = true)
@BsonDiscriminator(key = "type", value = "Items")
@AllArgsConstructor
@NoArgsConstructor
public class BorrowItem extends Borrow{

    private String itemName;

    private String description;

    @Override
    public String getType() {
        return "Items";
    }
}

Reminder Document Structure

@Data
@Builder
@Document(collection = "reminders")
public class Reminder {
    @Id
    private String id;

    @DocumentReference
    private Borrow borrow;

    private String message;

    private String header;

    @Indexed
    private String borrower;

    @Indexed
    private String borowee;

    private boolean read;

    @CreatedDate
    private Date createdAt;

    @LastModifiedDate
    private Date updatedAt;
}

I am trying to fetch all the reminders of the currently logged in user by using the id of the user. I followed the official spring-data-mongo documentation for understanding the property expression query.

I wrote the following method in the ReminderRepository.

@Repository
public interface ReminderRepository extends MongoRepository<Reminder, String> {

    List<Reminder> findByBorrowBorrowerId(String id);
}

However, executing this always returns 0 results although the records are already present.

2024-03-30 17:05:26.998 DEBUG 27280 [nio-3080-exec-4,6607f8fe755cc27c4afe5cfe81fb8f8b,75158ed32fd3ebcc] o.s.d.m.c.MongoTemplate : find using query: { "borrow" : { "$oid" : "6605a6ea9796e7405763c9ac"}} fields: Document{{}} for class: class com.kitaab.hisaab.ledger.entity.Reminder in collection: reminders

I am seeing that the above query is getting generated by spring-data-mongo and the id 6605a6ea9796e7405763c9ac here is actually of the user not of the borrow.

As per the documentation it should have taken the property path borrow.borrower.id.

Also tried with @Query("{ 'borrow.borrower.id' : ?0 }}") annotation. However, no breakthrough.

Help me resolve this issue.

Repository link.

Edit 1:

Just the reproducer code

0

There are 0 answers