Search code examples
javaspringhibernateroo

How do I can pass userDetails as a parameter


How do I pass Userr instance as a parameter. I have tried this but it's not working.

public Double getCurrentProfitAndLoss() {
    Double currentProfitAndLoss = null;
    Userr user = ( (OptionsUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUserr();
    user =  Userr.findUserr(user.getId());
    HomePageService homePageService = homePageServiceFactory.getObject();

    System.out.println("homePageService object  created");
    try {
        currentProfitAndLoss = homePageService.getCurrentProfitAndLoss(user);
    } catch(Exception e) {
        e.printStackTrace();
    }
    return currentProfitAndLoss;
}

But when I pass as a single property of Userr like this it works fine.

public Double getCurrentProfitAndLoss() {
    Double currentProfitAndLoss = null;
    Userr user = ( (OptionsUser)     SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUserr();
    user =  Userr.findUserr(user.getId());
    HomePageService homePageService = homePageServiceFactory.getObject();

    System.out.println("homePageService object  created");
    try {
        currentProfitAndLoss = homePageService.getCurrentProfitAndLoss(user.getId());
    } catch(Exception e) {
        e.printStackTrace();
    }
    return currentProfitAndLoss;
}

How do I pass user object?

This is the method accepting user object and I am not getting any error.

public class HomePageService {

public Double getCurrentProfitAndLoss(Userr user) {
 System.out.println("iside HOmepageService");
 Double currentProfitPercPA =       StrategyExitReport.findCurrentProfitAndLossById(user);
 System.out.println("iside currentProfitPercPA" + currentProfitPercPA);
 return currentProfitPercPA;
 }
}

Solution

  • I am assuming that you use jpa. So you defined the method findCurrentProfitAndLossById that is ends with ById but you actually use the object. So change the name of the method to

    findCurrentProfitAndLossByUser
    

    have a look at this site http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.query-methods.query-creation