将对象添加到play框架中的列表

时间:2012-04-27 10:23:19

标签: list jpa playframework

我是新玩的,每当我使用list.add(Object)到列表并打印列表的大小时,它仍然是0 !!!

我的方法是喜欢一个教程,它检查登录用户之前是否喜欢过这个教程,如果是,它会将教程的likeCount增加一个,并将教程添加到用户的类似列表中。如果不是,那就表明他已经喜欢它了。 由于教程没有保存在列表中,我无法检查它是否已被喜欢!!!

型号:

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

控制器: 公共教程扩展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>  

2 个答案:

答案 0 :(得分: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());
} 

答案 1 :(得分: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());

}