1

This is my model I want to find users in the BusinessInformation object by partnerAssigned but am confused how to do it .Able to do it with name or anything else not a list or object

    public class Entity {

    @Id
    private String id;
    private String username;
    private String name;
    private String surname;
    private Address address;
    private BusinessInformation businessInformation;
    private Binary image;
    private List<UserRolls> userRolls;

//Constructor 

//Getters and Setters

//BusinessInformation  Model
private String businessName;
    private String businessType;
    private String businessSize;
    private String telephoneNumber;
    private String registrationNumber;
    private String productAssigned;
    private String partnerAssigned;
    private String salesmenEmail;
//Constructor 

//Getters and Setters
    List<User> findUsersByPartnerAssigned(String partnerAssigned);

In my repository I am tryng something like this

@Query("{ 'partnerAssigned' : ?0 }")
    List<User> findUsersByPartnerAssigned(String partnerAssigned);

And my Controller

//Get all Entities by type
@RequestMapping(value = "/{partner}", method = RequestMethod.GET)
public List<User> getAllByPartner(@PathVariable("partner") String partner) {
    List<User> user = this.entityRepository.findUsersByPartnerAssigned(partner);
    return user;
}

Any advice will be appreciated

2 Answers2

0

You can use nested properties traversal as explained here in the docs. The example given is Person -> Address -> ZipCode which is similar structure to your problem. Also you can use direct query ( using joins since BusinessInformation is referenced in Entity). See here for an example.

Shailendra
  • 8,874
  • 2
  • 28
  • 37
  • Hi thanks for responding , how would the controller look for something like this –  Jun 20 '21 at 12:14
  • Trying to use a path variable way but ,doesnt seem to be working @RequestMapping(value = "/{partner}", method = RequestMethod.GET) public List getAllByPartner(@PathVariable("partner") String partner) { List user = this.entityRepository.findByBusinessInformation_PartnerAssigned(partner); return user; } –  Jun 20 '21 at 12:23
0

You can simply use List<Entity> findByBusinessInformation_partnerAssigned(String name);

Hope you have annotated

@Document
public class Entity{
}

And Repository

public EntityRepository extends MongoRepository<Entity,String>{}
varman
  • 8,704
  • 5
  • 19
  • 53