1

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

@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;
    }
}

你能帮我如何做删除功能和ondelete级联未来。谢谢维贾亚库马尔

4

1 回答 1

1

当你使用CascadeType.DELETE

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

当一个对象被传递给 delete()时,它告诉 Hibernate 导航label关联并删除持久实例。ModuleImpl

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

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

使用,如果被持久性引用all-delete-orphan,任何新实例化Label的都将成为持久性。如果删除时引用了任何持久性,则将删除它。如果从持久性的集合中删除任何持久性,也将被删除。LabelModuleLabelModuleModuleLabellabelModule

于 2013-09-17T07:59:09.460 回答