Play Framework Ebean始终返回一对多关系中的空列表

时间:2014-05-08 21:20:35

标签: playframework playframework-2.2 ebean

我正在使用游戏2.2.2,我在一对多关系中遇到列表问题..

我有这个当前设置

@Entity
public class BD_Uno extends Model {
    @Id
    public int serial;

    @OneToMany(mappedBy = "bd_uno", cascade = CascadeType.ALL)
    public List<BD_DOS> bd_dos = new ArrayList<BD_DOS>();

    public static Finder<Integer,BD_Uno> find = new Finder<Integer,BD_Uno>(Integer.class, BD_Uno.class);
}

还有:

@Entity
public class BD_DOS extends Model {

  @Id
  public int serial;

  @ManyToOne
  public BD_Uno bd_uno;

}

当前的问题是,当我想获得一对多关系的列表时,它似乎总是为空:

    BD_Uno uno= new BD_Uno();
    BD_DOS dos = new BD_DOS();
    dos.serial = 2;
    dos.save();

    BD_DOS dos_dos =  new BD_DOS();
    dos_dos.serial = 3;
    dos_dos.save();     

    uno.serial = 1;
    uno.bd_dos.add(dos);
    uno.bd_dos.add(dos_dos);

    uno.save();

    BD_Uno test = BD_Uno.find.byId(1);

在这种情况下,当我看到test.bd_dos时,它总是为空! 我做错了什么,我想这是非常愚蠢的事情,但让我头疼!

感谢您的时间, 克劳迪奥

1 个答案:

答案 0 :(得分:0)

不要在这里初始化bd_dos

@OneToMany(mappedBy = "bd_uno", cascade = CascadeType.ALL)
public List<BD_DOS> bd_dos = new ArrayList<BD_DOS>();

相反,您可以尝试以下方法:

@OneToMany(mappedBy = "bd_uno", cascade = CascadeType.ALL)
public List<BD_DOS> bd_dos;

...

BD_Uno uno= new BD_Uno();
uno.serial = 1;    
uno.bd_dos = new ArrayList<BD_DOS>();

BD_DOS dos = new BD_DOS();
dos.serial = 2;
// relate dos back to bd_uno  
// this should be optional since Ebean does this on its own  
dos.bd_uno = uno;

BD_DOS dos_dos =  new BD_DOS();
dos_dos.serial = 3;
dos_dos.bd_uno = uno;

uno.bd_dos.add(dos);
uno.bd_dos.add(dos_dos);

uno.save();