Hibernate继承更新问题

时间:2011-08-31 14:09:16

标签: java hibernate jpa

我有以下Pojos:

@Entity
@Table(name = "COMMUNICATION")
@Audited
public class CommunicationPojo
    implements Serializable
{
    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "TRANSMISSION_TYPE", nullable = false, unique = true)
    private TransmissionTypePojo transmissionType;
}


@Entity
@Table(name = "TRANSMISSION_TYPE")
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
@Audited
public abstract class TransmissionTypePojo
    implements Serializable
{
    @Id
    @GeneratedValue(strategy = GenerationType.TABLE)
        private long id;
    @OneToOne(mappedBy= "transmissionType")
    private CommunicationPojo communication;
}


@Entity
@Table(name = "TRANSMISSION_TYPE_AS2")
@Audited
public class TransmissionTypeAS2Pojo
    extends TransmissionTypePojo
    implements Serializable
{}

@Entity
@Table(name = "TRANSMISSION_TYPE_MAIL")
@Audited
public class TransmissionTypeMailPojo
    extends TransmissionTypePojo
    implements Serializable
{}

如果保存了与TRANSMISSION_TYPE_MAIL实例的通信实例,更改了通信实例,使其包含TRANSMISSION_TYPE_AS2实例而不是TRANSMISSION_TYPE_MAIL实例并更新了通信,则hibernate会在TRANSMISSION_TYPE_AS2中插入一个条目,但不会删除旧条目在TRANSMISSION_TYPE_MAIL中。 是否有可能通过hibernate自动删除旧条目,还是我必须自己删除它?

1 个答案:

答案 0 :(得分:0)

您可以将orphanRemoval=true添加到communication的OneToOne注释中。

请注意,它与继承无关。