Hibernate删除父项而不删除子项

时间:2014-03-15 04:48:00

标签: java hibernate hibernate-mapping

嗨,有没有人可以解释一下如何删除父母删除孩子的情况。例如当我有一个名为Class和Student的实体时。父母是班级,孩子是学生。我想删除父(Class)没有删除Child作为student.In Hibernate。

ClassEntity

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.myapp.struts;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;

/**
 *
 * @author hyva
 */
@Entity
public class Calss implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String name;
    @OneToMany
    @Cascade(CascadeType.ALL)   
    @JoinColumn(name = "CalssId")        
    private Set<Student> student = new HashSet();

    public boolean addstudent(Student s){
    return student.add(s);
    }

    public Long getId() {
        return id;
    }

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

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Calss)) {
            return false;
        }
        Calss other = (Calss) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.myapp.struts.Calss[ id=" + id + " ]";
    }

    public String getName() {
        return name;
    }

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

    public Set<Student> getStudent() {
        return student;
    }

    public void setStudent(Set<Student> student) {
        this.student = student;
    }



}

学生

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.myapp.struts;

import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

/**
 *
 * @author hyva
 */
@Entity
public class Student implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String studentname;
    private String age;

    public String getStudentname() {
        return studentname;
    }

    public void setStudentname(String studentname) {
        this.studentname = studentname;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public Long getId() {
        return id;
    }

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

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Student)) {
            return false;
        }
        Student other = (Student) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.myapp.struts.Student[ id=" + id + " ]";
    }

}

1 个答案:

答案 0 :(得分:0)

对象具有不同类型的关联。例如,复合关联,其中它们将是父对象,并且将控制一个或多个子/依赖对象。 依赖对象没有自己的生命周期和身份,如果没有父对象,它就不能单独存在。

对于父对象发生的任何修改(如创建,更新,删除)都会发生,并且它也会发生在子对象上。例如,如果我正在删除父对象,则删除不应该仅发生在父对象上,但它应该删除所有这个子对象,因为这些子对象生命周期完全取决于父对象。它们是JPA和Hibernate中的级联策略,用于实施此方法。

回到你的问题。我假设Employee是你的父母,而phone是它的子类。没有任何意义让手机/儿童物品不被删除。这实际上打破了对象之间的关联方法。

您仍然可以通过执行HQL或本机SQL quires来完成任务,但不建议这样做。

希望这有用!