【发布时间】:2014-05-04 19:57:09
【问题描述】:
首先,我正在使用使用 Ebeans 的 Playframework 2.2.2 编写一个 Web 应用程序。 我将从代码开始,因为这样更容易解释我的问题是什么:
这是我的模型之一:
@Entity
public class Topic extends Model {
@Id
private String title;
private String description;
@ManyToOne
private User createdBy;
private Integer popular;
@Formats.DateTime(pattern="dd/MM/yyyy")
private Date created;
private boolean over18;
@ManyToMany(cascade = CascadeType.REMOVE)
private List<Tag> tags;
}
模型有这个方法:
/**
* Return the number of opinions that exist on a specific Topic
*/
public int countOpinions() {
return Opinion.find.where().eq("topic", this).findRowCount();
}
这是我的第二个相关模型:
@Entity
public class Opinion extends Model {
@Id
private Long opinionId;
@Constraints.Pattern("pos|neg|neut")
private String type;
@ManyToOne
private Topic topic;
@ManyToOne
private User user;
private int rating;
private String text;
private boolean reported;
@ManyToOne
private Opinion parent;
}
包含这个静态方法
public static Model.Finder<Long, Opinion> find = new Model.Finder<Long, Opinion>(Long.class, Opinion.class);
这里我们在 HTML 中调用
@topics.map { topic =>
<th scope="col" id="name">@topic.getTitle()</th>
<th scope="col" id="description">@topic.getDescription()</th>
<th scope="col" id="opinions">@topic.countOpinions()</th>
}
问题:
好的,所以 countOpinions() 无法正常工作。我创建了一些测试值,它应该显示特定测试主题的值 2,但它显示值 0。我不确定 Ebeans 是如何使用我为自己创建的类型保存这些字段的,但是 afaik 它应该像这样工作.
我已经尝试过覆盖模型中的 equals 方法,因为我认为它可能会被使用,但显然它不是。
感谢每一个帮助。
【问题讨论】:
标签: html jpa playframework entity ebean