List 在play framework中将对象添加到列表
我是新手,每当我使用列表时。将(对象)添加到列表并打印列表的大小,它仍然是0 我的方法是喜欢一个教程,它检查登录的用户以前是否喜欢这个教程,如果喜欢,它将教程的likeCount增加1,并将教程添加到用户的like列表中。如果没有,则表示他已经喜欢它。 由于教程没有保存在列表中,我无法检查它是否已经被喜欢了 型号:List 在play framework中将对象添加到列表,list,jpa,playframework,List,Jpa,Playframework,我是新手,每当我使用列表时。将(对象)添加到列表并打印列表的大小,它仍然是0 我的方法是喜欢一个教程,它检查登录的用户以前是否喜欢这个教程,如果喜欢,它将教程的likeCount增加1,并将教程添加到用户的like列表中。如果没有,则表示他已经喜欢它。 由于教程没有保存在列表中,我无法检查它是否已经被喜欢了 型号: @Entity public class RegisteredUser extends Model { public String name; @ManyTo
@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;
}
观点:
<a href="@{Tutorials.likeTutorial()}">Like </a>
+您不需要调用this.save()
和this.likeList=newArrayList()构造函数中的代码>。实际上,后者在语法上是错误的
+将教程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());
}
我做了你上面提到的所有调整
及
RegisteredUser模型中的映射
@ManyToMany
public List<Tutorial> likeList;
}故障部件的代码在哪里,user.updatelkelist(tut)@OHAssan我的意思是我们需要查看user.updateLike的代码,因为这就是fails@PereVillega这里是公共的void更新列表(教程t){this.likeList.add(t);this.refresh();}
@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());