2016-08-22 9 views
0

Userrインスタンスをパラメータとして渡すにはどうすればいいですか?私はこれを試しましたが、うまくいきません。userDetailsをパラメータとして渡すにはどうすればいいですか

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; 
} 

しかし、このようにUserrの単一のプロパティとして渡すとうまくいきます。

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; 
} 

ユーザオブジェクトを渡すにはどうすればよいですか?

これはユーザーオブジェクトを受け入れる方法であり、エラーは発生しません。

パブリッククラスHomePageService {

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

ほとんどの場合、あなたがHomePageServiceの各メソッドのシグネチャを変更する必要が見ています。 IntegerまたはLongの代わりにUserrを受け入れるようにします(またはgetId()が返すすべての型)。 – Florian

+0

私はユーザーオブジェクトを受け入れるメソッドを持っています。しかし、それでも動作しませんしかし、私はuser.getID()を渡し、ロングとしてそれを受け入れ動作します。しかし、Userオブジェクトを渡してパブリックとして受け入れると、Double getCurrentProfitAndLoss(Userr user)は動作しません。 –

+0

あなたの問題の理由を見つけることができるようにHomePageServiceも追加することができます。読みやすさを考慮して適切に挿入してください。 – Florian

答えて

0

私はあなたがJPAを使用することを想定しています。そこで、ByIdで終わるfindCurrentProfitAndLossByIdメソッドを定義しましたが、実際にはそのオブジェクトを使用しています。 だから、

findCurrentProfitAndLossByUser 

にメソッドの名前を変更するには、このサイトhttp://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.query-methods.query-creation

+0

ありがとう:)それは今働いています:) tysm –

関連する問題