hibernate更新子表而不是将记录附加到子表

时间:2017-11-10 20:57:32

标签: java hibernate

@Antoniossss

记录将被插入到Wslog表中,但错误输出为Emp。它还试图插入对象wslog。

-D

WsLog(主)

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

EMPS(儿童)

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

Repositiory

    @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;

服务实施

 public interface WsLogRepository extends CrudRepository<WsLog, Long> {

 }

1 个答案:

答案 0 :(得分: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

你没有设置手动ID等。 还要在标识符字段上使用@Id注释。