【问题标题】:JPA (EBeans) Errors with RelationsJPA (EBeans) 关系错误
【发布时间】: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


    【解决方案1】:

    我测试很快,效果很好

    private FakeApplication fakeApplication;
    
    @Before
    public void setup() {
        fakeApplication = fakeApplication(inMemoryDatabase());
        start(fakeApplication);
    }
    
    @After
    public void tearDown() {
        stop(fakeApplication);
    }
    
    @Test
    public void countOpinionsTest() {
        // given
        Topic topic = new Topic();
        topic.setTitle("test");
        topic.save();
        Opinion opinion1 = new Opinion();
        opinion1.setOpinionId((long) 1);
        opinion1.save();
        Opinion opinion2 = new Opinion();
        opinion2.setOpinionId((long) 2);
        opinion2.save();
        // when
        opinion1.setTopic(topic);
        opinion1.update();
        opinion2.setTopic(topic);
        opinion2.update();
        // then
        assertThat(topic.countOpinions()).isEqualTo(2);
    }
    

    您是否设置了您的 ebean 配置和相应包中的实体(此处为模型)?

    db.default.driver=org.h2.Driver
    db.default.url="jdbc:h2:mem:play"
    ebean.default="models.*" 
    

    【讨论】:

    • 刚刚做了一个类似的JUnit测试,也可以。很奇怪,因为我对我的 Junit 测试使用相同的测试数据库数据,就像渲染的 Web 应用程序一样。嗯...顺便说一句,我确实使用了这些设置。编辑:发现错误,我在数据库的测试值中出错了。谢谢帮助
    猜你喜欢
    • 2015-08-18
    • 1970-01-01
    • 1970-01-01
    • 2011-07-21
    • 2021-09-22
    • 2020-04-15
    • 1970-01-01
    • 1970-01-01
    • 2016-01-14
    相关资源
    最近更新 更多