2017-10-19 215 views
0

對我的英語提前感到抱歉。 我試圖在實體列表中添加元素,並且只有第一個元素停留在此列表中嘗試將元素添加到JPA實體列表時出現奇怪的行爲:只有第一個元素持續存在

下面的例子:

Project project = project_facade.find(1L); 
Machine machine = machine_facade.find(1L); 

for(int i=0;i<3;i++){ 
    Test test = new Test(); 
    test.setName("Test "+i); 

    test.setProject(project); 
    test.setMachine(machine); 

    project.getTestList().add(test); 
    machine.getTestList().add(test); 
} 

project.getMachineList().add(machine); 
machine.getProjectList().add(project); 

project_facade.edit(project); 

此,在數據庫中,該項目和設備是否正確連接(通過映射表),但測試表只包含記錄「測試0」後( project_id字段和machine_id字段是正確的)。

我不明白爲什麼它是隻有第一單元工作...

感謝您的幫助!

項目實體:

@Entity 
public class Project implements Serializable { 

    private static final long serialVersionUID = 1L; 

    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    @Basic(optional = false) 
    @Column(name = "id") 
    private Long id; 

    @Basic(optional = false) 
    @NotNull 
    @Size(min = 1, max = 45) 
    @Column(name = "name") 
    private String name; 

    @ManyToMany(mappedBy = "projectList", cascade = CascadeType.ALL) 
    private List<Machine> machineList; 

    @OneToMany(mappedBy = "project", cascade = CascadeType.ALL *SOLUTION : ,fetch = FetchType.EAGER*) 
    private List<Test> testList; 

    //constructor, getters, setters 
} 

機實體:

@Entity 
public class Machine implements Serializable { 

    private static final long serialVersionUID = 1L; 

    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    @Basic(optional = false) 
    @Column(name = "id") 
    private Long id; 

    @Basic(optional = false) 
    @NotNull 
    @Size(min = 1, max = 45) 
    @Column(name = "name") 
    private String name; 

    @JoinTable(name = "project_machine", joinColumns = { 
     @JoinColumn(name = "machine", referencedColumnName = "id")}, inverseJoinColumns = { 
     @JoinColumn(name = "project", referencedColumnName = "id")}) 
    @ManyToMany 
    private List<Project> projectList; 

    @OneToMany(mappedBy = "machine",cascade = CascadeType.ALL) 
    private List<Test> testList; 

    //constructor, getters, setters 
} 

測試實體:

@Entity 
public class Test implements Serializable { 

    private static final long serialVersionUID = 1L; 

    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    @Basic(optional = false) 
    @Column(name = "id") 
    private Long id; 

    @Basic(optional = false) 
    @NotNull 
    @Size(min = 1, max = 64) 
    @Column(name = "name") 
    private String name; 

    @JoinColumn(name = "project", referencedColumnName = "id") 
    @ManyToOne(optional = false) 
    private Project project; 

    @JoinColumn(name = "machine", referencedColumnName = "id") 
    @ManyToOne(optional = false) 
    private Machine machine; 

    //constructor, getters, setters 
} 

然後我所有的外牆伸出AbstractFacade這裏編輯方法是:

public void edit(T entity) { 
     getEntityManager().merge(entity); 
    } 
+0

請向我們展示這兩個實體以及執行'project_facade.edit()' – crizzis

+0

當然,做完了 – Nep

+0

'getTestList()'?第一個元素應該實際上是「測試0」嗎? – pirho

回答

0

我找到了答案!!!!!我在Project實體的OneToMany註解中添加了「fetch = FetchType.EAGER」!

相關問題