2017-11-10 66 views
0

@Antoniossss休眠更新子表而不是將記錄追加到子表

記錄正在插入到Wslog表中,但正在爲Emp錯誤。它也試圖插入對象wslog。

insert into wslog (totrecords, logid) values (?, ?) 
insert into emp (sid, logid, **wslog**, eid, ename) values (?, ?, ?, ?, ?, ?, ?) 

WsLog(主)

@Column(name = "logid") //has sequence 
private long logid; 
@Column(name = "totrecords") 
private long totrecords; 
@OneToMany(mappedBy="wslog") 
private List<Emps> emplist; 

EMPS(兒童)

@Id 
    @Column(name = "sid") //has sequence 
    private long sid; 
    @Column(name = "logid") 
    private long logid; 
    @Column(name = "eid") 
    private Long eid; 
    @Column(name = "ename") 
    private String ename; 
    @ManyToOne(cascade = CascadeType.ALL) 
    @JoinColumn(name = "logid") 
    private Wslog wslog; 

Repositiory

public interface WsLogRepository extends CrudRepository<WsLog, Long> { 

} 

服務實現

my.domain.client.model.Wslog wsLog = new Wslog(); 
    wsLog.setTotrecords(getTotrecords()); 
    List<my.domain.client.model.Employees> empList = new ArrayList<my.domain.client.model.Employees>(); 
    for (my.domain.client.model.Employees emp : getEmployees()) { 
     my.domain.client.model.Employees emps = new Employees();    
     emps.setID(getEid()); 
     emps.setEname(getEname()); 
     emps.setLog(wsLog); 
     empList.add(emps); 
    } 
    wsLog.setEmployees(empList); 
    wsLogRepository.save(wsLog); 
+0

哪一列是emp表中的主鍵? – FuSsA

+0

爲什麼你會設置'LogId'保證'Log'對象是另一個神祕的 – Antoniossss

回答

0

你的映射是錯誤的,

EMP

@ManyToOne 
@JoinColumn(name = "logid") 
private WsLog log; 

WsLog

@OneToManny(mappedBy="log) 
private List<Emp> employees; 

這是你如何讓關係

Employee emp=new Employee(); 
emp.setLog(log); // log is of type WsLog ofcourse 

你不手動設置IDS等 還有R請記得使用@Id註釋上的標識符字段。

+0

我仍然有問題。我用更新後的代碼修改了我的主POST。 – rookieDeveloper