2012-04-27 77 views
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; 
    } 

控制器: 公共教程延伸控制器{ 公共靜態無效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> 
+0

哪裏的失敗,user.updateLikeList(嘖)的部分的代碼; ? – 2012-04-27 11:51:01

+0

@Pere Villega是的! – 2012-05-04 22:42:56

+0

@OHAssan我的意思是,我們需要看到user.updateLike的代碼,因爲這就是在這裏失敗 – 2012-05-04 23:04:21

回答

0

+您無需在構造函數中調用this.save()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()); 
} 
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()); 

}

相關問題