我有两个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
本身。
是否有任何想法让它发挥作用?
答案 0 :(得分:0)
没关系。显然,当我尝试保存EmployeeProfile
时,expertises
中已有4个对象。删除空对象(没有有效ID的对象)可以解决问题。