一对一连接表级联删除

时间:2014-05-28 00:16:58

标签: java hibernate jpa orm one-to-one

我有一个级联删除问题,在连接表上使用单向一对一关联。例如:

xml声明是:

<class name="Person">
<id name="id" column="personId">
    <generator class="native"/>
</id>
<join table="PersonAddress" 
    optional="true">
    <key column="personId" 
        unique="true"/>
    <many-to-one name="address"
        column="addressId" 
        not-null="true"
        unique="true"/>
</join>
</class>

<class name="Address">
<id name="id" column="addressId">
    <generator class="native"/>
</id>
</class> 

表格是

create table Person ( personId bigint not null primary key )
create table PersonAddress ( personId bigint not null primary key, addressId bigint not null   unique )
create table Address ( addressId bigint not null primary key )

我想实现当从person表中删除person时,personAddress表中的人也通过配置Hibernate XML声明来删除。我到处搜索,找不到解决方案。最后一个选择是手动编写删除代码,但我不喜欢它,如果Hibernate可以支持它。请帮忙。谢谢!

1 个答案:

答案 0 :(得分:2)

您应该能够将级联添加到多对一元素:

<many-to-one name="address"
    column="addressId" 
    not-null="true"
    unique="true"
    cascade="delete"

/&GT;