Hibernate:删除孩子也想删除父母?

时间:2016-04-05 20:48:25

标签: hibernate hsqldb

我的应用程序保留了RECosts,每个都分配给了Timerange。时间范围分配给RealEstate。

当我创建时间范围然后立即尝试再次删除时,我得到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;
    }

}

1 个答案:

答案 0 :(得分:0)

该协会有

cascade = CascadeType.ALL

因此,每个操作(包括remove() / delete())都会级联到关联的RealEstate。