Ebean中多个级联仍然存在

问题描述 投票:0回答:1

我有一个模型类,它定义了属于同一类模型的子代的列表。用一些最初的子级对象保留一个新对象很好用,但是当我有两个或多个子级级别时,Ebean似乎无法很好地处理它。这似乎是意外的,所以我担心自己犯了一个错误。同时,我找不到任何有关多级持久级联的示例或提及,因此我的问题是:我的代码中有错误吗,这甚至是受支持的功能还是我发现了错误?

我的模特班:

@Entity
public class TestEntity extends Model {
    @Id
    private int id;
    private String text;
    @ManyToOne
    private TestEntity parentEntity;
    @OneToMany(cascade = CascadeType.ALL)
    private List<TestEntity> childEntities;
...
}

我的程序:


TestEntity grandparent = new TestEntity();
grandparent.setText("grandparent");
TestEntity parent = new TestEntity();
parent.setText("parent");
TestEntity child = new TestEntity();
child.setText("child");
grandparent.setChildEntities(Collections.singletonList(parent));
parent.setChildEntities(Collections.singletonList(child));
grandparent.save();

我为sql语句添加了日志记录,很明显,第三个插入未获得parent_entity_id的正确值。该行失败,因为0不是有效的外键,并且该批次已还原。

insert into test_entity (text, parent_entity_id) values ('grandparent',null);
insert into test_entity (text, parent_entity_id) values ('parent',1);
insert into test_entity (text, parent_entity_id) values ('child',0);

我正在将Play框架2.7.3与ebean插件版本5.0.2和Ebean版本11.39结合使用

java playframework cascade ebean
1个回答
0
投票

这确实是受支持的功能,并且上面的代码段应该可以保留所有三个实体。有unit test added可以验证它在最新版本的ebean中是否正常运行。

在ebean 11.39(当前是play框架最新支持的版本中,测试失败。使用该版本时,一个简单的解决方法是使用Long而不是原始int作为模型的ID。

虽然不能回答这个特定问题,但很高兴知道,如果在不使用ebean增强的setter的情况下设置了集合,也会出现这些相同的症状。使用公共字段和play enhancer时遇到了一些麻烦。

© www.soinside.com 2019 - 2024. All rights reserved.