由于错误的重复ID检测,无法加载夹具

Kip*_*riz 1 playframework

我正在尝试在应用程序启动期间借助Fixtures.loadModels()加载数据(@OnApplicationStart)我的数据文件非常简单,但我不断收到有关重复ID的错误.但是,根本没有重复的ID.

Category(auto):
  idEng: "auto"
  title: "Auto"

Group(crossover):
  idEng: "crossover"
  title: "Crossovers"
  parentCategory: auto
Run Code Online (Sandbox Code Playgroud)

相应的类是:

@Entity
public class Category extends Model {

    public String idEng;

    public String title;

    @OneToOne
    public Category parentCategory;
}
@Entity
@Table(name="ItemGroup")
public class Group extends Model {

    @Column(length=64)
    public String idEng;

    @Column(length=64)
    public String title;

    @ManyToOne
    public Category parentCategory; 
}
Run Code Online (Sandbox Code Playgroud)

有时候它在我开始游戏之后才有效,但是第二次加载数据肯定是有效的.我收到错误:

RuntimeException occured : Cannot load fixture categories.yml: 
Cannot load fixture categories.yml, duplicate id 'auto' for type models.Category 
Run Code Online (Sandbox Code Playgroud)

有人知道这里有什么问题吗?以前有人遇到过这样的问题吗?

Kip*_*riz 5

我稍微调试了一下,找到了原因:在Fixtures.idCache中播放加载实体的id.所以在启动后的第一次它工作正常,因为缓存是空的.但是,当Play重新创建数据库并尝试第二次加载数据时,缓存已包含给定的ID,而Play会抛出异常.它发生在下面的一段代码中(Fixtures.loadModels(String name),第185-187行):

if (idCache.containsKey(type + "-" + id)) {
   throw new RuntimeException("Cannot load fixture " + name + ", duplicate id '" + id + "' for type " + type);
}
Run Code Online (Sandbox Code Playgroud)

要清除缓存,必须调用Fixtures.delete()或Fixtures.deleteDatabase().这些方法清除了idCache变量.

为了解决我的问题,我刚刚在Fixtures.loadModels()之前添加了Fixtures.delete()的调用