2014-01-23 40 views
0

我有ManyToMany關係的2個模型。試了這個,它的工作原理:ManyToMany bean模型沒有Id值

@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 [email protected] 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

沒關係。顯然,當我試圖保存EmployeeProfile時,我已在expertises中有4個對象。刪除空的對象(沒有有效的ID)解決了這個問題。

相關問題