【发布时间】:2012-04-27 10:23:18
【问题描述】:
我是新手,每当我使用 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;
}
控制器: 公共教程扩展控制器{ 公共静态无效 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>
【问题讨论】:
-
失败部分的代码在哪里,user.updateLikeList(tut); ?
-
@OHAssan 我的意思是我们需要查看 user.updateLike 的代码,因为那是失败的
-
@Pere Villega 这里是 public void updateLikeList(Tutorial t) { this.likeList.add(t); this.refresh(); }
标签: list jpa playframework