ManyToMany bean模型没有Id值

时间:2014-01-23 22:03:21

标签: playframework-2.0 many-to-many ebean

我有两个ManyToMany关系的模型。试过这个并且它有效:

@Entity
public class EmployeeProfile extends Model {

    @Id
    public Long id;

    @ManyToMany
    public List<Expertise> expertises;
}

@Entity
public class Expertise extends Model {

    @Id
    public Long id;
    @Constraints.Required
    public String name;
}

是。在这个模型中,List<EmployeeProfile>方面没有Expertise但它有效。

即使expertises为空,这也不起作用:

@Entity
public class EmployeeProfile extends Model {

    @Id
    public Long id;

    @ManyToMany
    public List<Expertise> expertises;
}

@Entity
public class Expertise extends Model {

    @Id
    public UUID id;
    @Constraints.Required
    public String name;
}

显示ManyToMany bean models.Expertise@344eba6f does not have an Id value.

仅当expertises为空时才有效:

@Entity
public class EmployeeProfile extends Model {

    @Id
    public Long id;

    @ManyToMany(cascade = CascadeType.PERSIST)
    public List<Expertise> expertises;
}

@Entity
public class Expertise extends Model {

    @Id
    public UUID id;
    @Constraints.Required
    public String name;
}

在第三个示例中,当expertises不为空时,它显示重复错误。显然,当我尝试保存EmployeeProfile对象并且对象在expertises中有对象时,即使我只想保存Expertise,它也会尝试保存EmployeeProfile_Expertise对象关系,而不是Expertise本身。

是否有任何想法让它发挥作用?

1 个答案:

答案 0 :(得分:0)

没关系。显然,当我尝试保存EmployeeProfile时,expertises中已有4个对象。删除空对象(没有有效ID的对象)可以解决问题。