Hibernate:删除子项也想删除父项?
Hibernate: delete of child wants to delete parent as well?
我的应用程序保留 RECosts,每个都分配给一个时间范围。时间范围已分配给房地产。
当我创建一个 Timerange 然后立即尝试再次删除它时,我得到一个 java.sql.SQLIntegrityConstraintViolationException: integrity constraint violation: foreign key no action; FK_OY1P238K3TRS850XETM1STE4P table: RECOST
我不明白,因为在这个阶段没有与该时间范围相关的费用。
但是根据日志,hibernate 也尝试删除 RealEstate,这是不希望的。这是为什么?
日志
22:31:50.428 [http-nio-8080-exec-3] DEBUG org.hibernate.SQL - delete from RETimerange where id=?
Hibernate: delete from RETimerange where id=?
22:31:50.429 [http-nio-8080-exec-3] DEBUG org.hibernate.SQL - delete from RealEstate where id=?
Hibernate: delete from RealEstate where id=?
域
@Entity
public class RETimerange {
private int id;
private Date datefrom;
private Date dateto;
private RealEstate realestate;
private String comment;
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Date getDatefrom() {
return datefrom;
}
public void setDatefrom(Date datefrom) {
this.datefrom = datefrom;
}
public Date getDateto() {
return dateto;
}
public void setDateto(Date dateto) {
this.dateto = dateto;
}
@ManyToOne(cascade = CascadeType.ALL, targetEntity = RealEstate.class, fetch=FetchType.EAGER)
@JoinColumn(name="fk_realestate")
public RealEstate getRealestate() {
return realestate;
}
public void setRealestate(RealEstate realestate) {
this.realestate = realestate;
}
public String getComment() {
return comment;
}
public void setComment(String comment) {
this.comment = comment;
}
}
协会有
cascade = CascadeType.ALL
因此,每个操作(包括 remove()
/delete()
)都级联到关联的 RealEstate。
我的应用程序保留 RECosts,每个都分配给一个时间范围。时间范围已分配给房地产。
当我创建一个 Timerange 然后立即尝试再次删除它时,我得到一个 java.sql.SQLIntegrityConstraintViolationException: integrity constraint violation: foreign key no action; FK_OY1P238K3TRS850XETM1STE4P table: RECOST
我不明白,因为在这个阶段没有与该时间范围相关的费用。 但是根据日志,hibernate 也尝试删除 RealEstate,这是不希望的。这是为什么?
日志
22:31:50.428 [http-nio-8080-exec-3] DEBUG org.hibernate.SQL - delete from RETimerange where id=?
Hibernate: delete from RETimerange where id=?
22:31:50.429 [http-nio-8080-exec-3] DEBUG org.hibernate.SQL - delete from RealEstate where id=?
Hibernate: delete from RealEstate where id=?
域
@Entity
public class RETimerange {
private int id;
private Date datefrom;
private Date dateto;
private RealEstate realestate;
private String comment;
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Date getDatefrom() {
return datefrom;
}
public void setDatefrom(Date datefrom) {
this.datefrom = datefrom;
}
public Date getDateto() {
return dateto;
}
public void setDateto(Date dateto) {
this.dateto = dateto;
}
@ManyToOne(cascade = CascadeType.ALL, targetEntity = RealEstate.class, fetch=FetchType.EAGER)
@JoinColumn(name="fk_realestate")
public RealEstate getRealestate() {
return realestate;
}
public void setRealestate(RealEstate realestate) {
this.realestate = realestate;
}
public String getComment() {
return comment;
}
public void setComment(String comment) {
this.comment = comment;
}
}
协会有
cascade = CascadeType.ALL
因此,每个操作(包括 remove()
/delete()
)都级联到关联的 RealEstate。