I have the below Offering class in a Java/Spring Boot app. IT has a @DBRef to Security (another document)
@Document(collection = "market")
@Getter
@Setter
@EqualsAndHashCode
@AllArgsConstructor
@NoArgsConstructor
public class Offering
{
@Id
String id;
@CreatedDate
Date createdDate;
@LastModifiedDate
Date modifiedDate;
@Field(name = "askQty")
Integer askQty;
@Field(name = "askPrice")
Double askPrice;
@Field(name = "askYTW")
Double askYTW;
@Field(name = "type")
String type;
@DBRef
Security security;
}
The Security class is:
@Document(collection = "secmaster")
@Getter
@Setter
@EqualsAndHashCode
@AllArgsConstructor
@NoArgsConstructor
public class Security
{
@Id
@Field(name = "_id")
private String cusip;
private String state;
private String desc;
private Double coupon;
...
}
Everything works as expected using OfferingRepository (which extends MongoRepository) , EXCEPT for sorting. Specifically, passing a property of the referenced class, Security, into sort params throws exceptions. Example, I can sort on ‘askQty’ (property of Offering) but not on ‘security.desc’ (property of Security).
Is there a recommended way to design such one-to-many associations and still be able to sort on properties of referenced ‘child’ entities? Seems like a very common design issue.