0

リストにlist.add(Object)を使用してリストのサイズを出力するときはいつでも、プレイするのは初めてですが、0のままです!!!

私の方法は、チュートリアルを高く評価することです。ログインしたユーザーが以前にこのチュートリアルを高く評価したかどうかを確認し、高く評価した場合は、チュートリアルのlikeCountを1つ増やし、ユーザーのいいねリストにチュートリアルを追加します。いいえの場合、それは彼がすでにそれを好きであるとレンダリングします。チュートリアルがリストに保存されていないため、すでに気に入っているかどうかを確認できません!!!

モデル:

    @Entity
    public class RegisteredUser extends Model {
public String name;
    @ManyToMany 
public List<Tutorial> contributedTutorials;

     public RegisteredUser(String name) {
     this.name = name;
     this.likeList = newArrayList<Tutorial>();
     this.save();
     }
     }

    @Entity
    public class Tutorial extends Model {
public  String Title;
    public int likeCount;
    public Tutorial(String title) {
    this.title = title;
    this.likeCount = 0;
    }

コントローラー:public Tutorials extends Controller {public static void likeTutorial(){

   if (session.get("RegisteredUserId") != null && session.get("tutID") != null ) {
    {   
        long uId = Long.parseLong(session.get("RegisteredUserId"));
        RegisteredUser user = RegisteredUser.findById(uId);
        long tId = Long.parseLong(session.get("tutID"));
        Tutorial tut = Tutorial.findById(tId);
        int x = tut.likeCount;
        x++;
        if (!(user.likeList.contains(tut))) 
            // to check that this user didn't like this tut before
        { 
            Tutorial.em().createQuery("update Tutorial set likeCount ="+ x +" where id=" +tId).executeUpdate();
            tut.refresh();
            user.updateLikeList(tut); // THIS IS NOT WORKING!!!
            renderText("You have successfully liked this Tutorial " + user.likeList.size());
        } 
        }
        renderText("Opps Something went Wrong!!");
    }
}
}

景色 :

     <a href="@{Tutorials.likeTutorial()}">Like </a>  
4

2 に答える 2

0

+コンストラクタでthis.save()andを呼び出す必要はありません。this.likeList = newArrayList<Tutorial>();実際、後者は構文的に間違っています。

+チュートリアル ID をセッション変数として渡すのは非常に間違っています。これを GET パラメータとしてアクションに渡す必要があります。

+小切手を次のものに置き換えます。

// to check that this user didn't like this tut before
if (! user.likeList.contains(tut)) {
    // Tutorial.em().createQuery("update Tutorial set likeCount ="+ x +" where id=" +tId).executeUpdate();
    // tut.refresh();
    // user.updateLikeList(tut); // THIS IS NOT WORKING!!!
    tut.likeCount++;
    tut.save();

    // Since it's a ManyToMany relationship, you only need to add it to one list and the other will reflect properly if mappedBy properly
    user.likeList.add(tut); // note that the example you showed uses contributedTutorials not likeList
    user.save();

    renderText("You have successfully liked this Tutorial " + user.likeList.size());
} 
于 2012-05-05T00:00:19.607 に答える
0

上記のすべての調整を行いました

RegisteredUser モデルのマッピング

@ManyToMany
public List<Tutorial> likeList;

チュートリアルモデルにマッピングを追加しました

    @ManyToMany  
    public List<RegisteredUser> likersList;

コントローラーのメソッドを次のように調整しました

if (! user.likeList.contains(tut)) {

tut.likeCount++; 
//tut.likersList.add(user); // however this raised an error too! at the next line
tut.save();
//as for the likeCount update, it has worked perfectly, Thank You

// Since it's a ManyToMany relationship, you only need to add it to one list and the other will reflect properly if mappedBy properly 
//I have modified Model Tutorial as shown above and it didn't work too !
user.likeList.add(tut); 
user.save(); // this raised an error!! 

renderText("You have successfully liked this Tutorial " + user.likeList.size());

}

于 2012-05-05T17:49:33.827 に答える