Ebean manyToMany未保存在事务中

时间:2014-03-14 09:28:13

标签: playframework-2.2 ebean

我正在使用Play-framework 2.2.1和Ebean。我和StudentClass之间有许多关系。

在交易中,当创建一个schoolClass,添加到Student并保存时,相应的桥表不会被填充。

@Entity
public class Student extends Model {
    ...

    @ManyToMany(cascade = CascadeType.ALL)
    public List<SchoolClass> schoolClasses = new ArrayList<SchoolClass>();
}

@Entity
public class SchoolClass extends Model {
     ...

     @ManyToMany(mappedBy = "schoolClasses")
     public List<Student> students = new ArrayList<Student>();
}

代码:

try {
    Ebean.beginTransaction();

    ...
    Student student = new Student();
    student.schoolClasses.add(schoolClass);
    student.save();

    Ebean.commitTransaction();
} finally {
    Ebean.endTransaction();
}

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

我曾经遇到过类似的问题,你可以尝试以下方法:

List<SchoolClass> classes = new ArrayList<SchoolClass>();
classes.add(schoolClass);
student.schoolClasses.addAll(classes);