如何用注释在hibernate中引入ondelete级联

时间:2013-09-17 07:30:57

标签: java hibernate hibernate-annotations

我是hibernate的新手。我要求在带有注释的hibernate中引入删除功能。即一旦我们删除了父级,它就需要删除子级记录,即我需要引入ondelete级联功能。你能帮忙介绍一下这个功能吗?请在下面找到java /实体代码。

@Entity
@Table(name = "atfLabel", uniqueConstraints = @UniqueConstraint(columnNames = {"key_", "module_id"}))
@SuppressWarnings("serial")
public class Label {
    private long id;
    private ModuleImpl module;
    private String key;

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)   
    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }

    @ManyToOne(optional=false)
    @JoinColumn(name="module_id", nullable=false, updatable=false)
    public ModuleImpl getModule() {
        return module;
    }
    public void setModule(ModuleImpl module) {
        this.module = module;
    }

    @Column(name="key_", length=160)
    public String getKey() {
        return key;
    }
    public void setKey(String key) {
        this.key = key;
    }   
}

@Entity
@Table(name = "atfModule")
@SuppressWarnings("serial")
public class ModuleImpl implements Module {
    private long id;
    private String name;
    private String description;

    @OneToMany(mappedBy="module")
    @Cascade(CascadeType.DELETE)
    private List<Label> label;

    @Override
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    @Override
    @Column(length=200, unique=true, nullable=false)
    public String getName() {
        return name;
    }

    @Override
    @Column(length=2000)
    public String getDescription() {
        return description;
    }

    @Override
    public void setName(String name) {
        this.name=name;
    }

    @Override
    public void setDescription(String desc) {
        this.description=desc;
    }
}

@Entity
@Table(name = "atfLabelText",
    uniqueConstraints = @UniqueConstraint(columnNames = { "label_id", "tenant_id", "locale" }))
@SuppressWarnings("serial")
public class LabelText {
    private long id;
    private Label label;
    private TenantImpl tenant;
    private String locale;
    private String text;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public long getId() {
        return id;
    }

    @ManyToOne(optional = false)
    @JoinColumn(name = "label_id", nullable = false, updatable = false)
    public Label getLabel() {
        return label;
    }

    public String getLocale() {
        return locale;
    }

    @ManyToOne(optional = true, fetch = FetchType.LAZY)
    @JoinColumn(name = "tenant_id", nullable = true, updatable = false)
    public TenantImpl getTenant() {
        return tenant;
    }

    @Column(length = 1500)
    public String getText() {
        return text;
    }

    public void setId(long id) {
        this.id = id;
    }

    public void setLabel(Label label) {
        this.label = label;
    }

    public void setLocale(String locale) {
        this.locale = locale;
    }

    public void setTenant(TenantImpl tenant) {
        this.tenant = tenant;
    }

    public void setText(String text) {
        this.text = text;
    }
}

你能帮我解决一下如何删除功能和删除级联未来。 谢谢 Vijaya Kumar

1 个答案:

答案 0 :(得分:1)

使用CascadeType.DELETE时:

@OneToMany(mappedBy="module")
@Cascade(CascadeType.DELETE)
private List<Label> label;

它告诉Hibernate导航label关联并在ModuleImpl的对象传递给delete()时删除持久化实例。

同样,如果您想要从Label关联中删除任何label持久性实例,则应该使用all-delete-orphan

@OneToMany(orphanRemoval = true, mappedBy = "module")
private List<Label> label;

使用all-delete-orphan,如果Label被持久Label引用,则任何新实例化的Module都会成为持久性的。如果LabelModule删除,则Module会引用任何永久性Label。如果从持久性label的{​​{1}}集合中删除任何持久性Module,也会将其删除。