Home > Net >  spring boot Cannot invoke Repository because Repository is null
spring boot Cannot invoke Repository because Repository is null

Time:10-14

public class UserList {

    private  String id;
    private String email;
    private String userType;
    private String rolls;
    private String partner;
    private Integer customersLinked;
    private String position;
    private String status;

    @Autowired
    ICustomerRepository customerRepository;

    public UserList (Users user){
        this.id = user.getId();
        this.email = user.getEmail();
        this.userType = user.getUserType();
        this.rolls = user.getRolls();
        this.partner = user.getPartner();

       List<Customer> customersLinked = customerRepository.findAllByLinkedUsersIn(user.getId());
        this.customersLinked = 0;
        this.position = user.getPosition();
        this.status =user.getStatus();
    }

    //Getter and Setter
}

This class is used as a list in the frontEnd ,get specific data ,not send all the data

 @RequestMapping(value = "usersLinked/{id}/{type}", method = RequestMethod.GET)
    public Object getUsersLinkedById(@PathVariable("id") String id,@PathVariable("type") Integer type) {
        List<String> users = null;
        switch (type) {
            case 0:
                users = usersRepository.findAll().stream().map(m -> m.getId()).collect(Collectors.toList());
                break;
        }
        //Add userList
      List<UserList> userList = new ArrayList<>();
            if(users != null)
                {
                    users.forEach(userId ->
                    {
                        Optional<Users> user = this.usersRepository.findById(userId);
                          userList.add(new UserList(user.get()));
                    });
            }
        return userList;
    }
}

As you can see from above I am calling al the data from the user repository and sending it the list

My customer repository

public interface ICustomerRepository extends MongoRepository<Customer, String> {


    Customer findByBusinessInformation_businessName(String businessName);

    List<Customer> findByBusinessInformation_partnerAssigned(String partnerAssigned);

    @Query("{ 'LinkedUsers' : ?0 }")
    Customer findByLinkedUsers(String id);

    List<Customer> findAllByLinkedUsersIn(String id);
}

In the userList I get the error when I add the logic wityh the customerRepository ,without the repository there everything is working(Want to use the repository to get an array of customer and then get the size() of the array and add it to linkedCustomers). Am I missing sommething

CodePudding user response:

You're probably missing the @repository annotation on top of your repository class.

Another unrelated word of advice:

In your controller you use findAll and filter in java to keep only the ids. Then you go to the same repository and perform another query per user-id from above. This is a causing you to create multiple database calls which are one of the most expensive operations you can do, when you already have all your data from the first single query...

Also if you're only looking at the bottom part of the function you don't event need a query per each user-id (when you have a list of user ids as input), you can create a query that uses the 'in' convention and pass a list of user-ids to create a single db call.

CodePudding user response:

First of all I would get rid of @Autowired ICustomerRepository customerRepository; in UserList class. It doesn't belong there. The counting of linked customers should be executed in ICustomerRepository and the result to be passed into UserList via the constructor.

e.g.

public class UserList {

    private  String id;
    private String email;
    private String userType;
    private String rolls;
    private String partner;
    private Long customersLinked; //better use Long instead of Integer
    private String position;
    private String status;


    // constructor takes the number of linked customers as parameter
    public UserList (Users user, Long customersLinked ) { 
        this.id = user.getId();
        this.email = user.getEmail();
        this.userType = user.getUserType();
        this.rolls = user.getRolls();
        this.partner = user.getPartner();
        this.customersLinked = customersLinked;
        this.position = user.getPosition();
        this.status =user.getStatus();
    }

    //Getter and Setter
}

and then create the count query in ICustomerRepository

e.g.

public interface ICustomerRepository extends MongoRepository<Customer, String> {
    //other methods

    Long countByLinkedUsersIn(String id); //not so sure if this query works in mongo
}

and finally in your controller

Optional<Users> user = this.usersRepository.findById(userId);
Long count = this.usersRepository.countByLinkedUsersIn(userId);
userList.add(new UserList(user.get(), count));

P.S. I have a doubt for the query method: Long countByLinkedUsersIn(String id);. Usually when repository methods have "In" in their names, countByLinkedUsersIn, then it is expected as parameter a List and not a single user id. However if your previous method List<Customer> findAllByLinkedUsersIn(String id); worked for you, then this one should work too.

CodePudding user response:

You are trying to inject the field customerRepository using Autowired annotation, but your class is not injectable.

  • You can add an annotation @Repository on your class UserList
  • Or use constructor injection (better way to inject your beans)
  • Related