前日,在Hbierante 下写一简单的父子关系的添加和删除程序,突然被一问题在为父类添加子类关系得时无论如何一次只能添加一条例如
Department de=new Department();
Personnel pe
=new Personnel();
Personnel pe2
=new Personnel();
pe.setDepartment(de);
pe2.setDepartment(de);
de.getPersonnels().add(pe);
de.getPersonnels().add(pe2);
dao.save(de);


总是只有第一个子类被插入进表了。后来终于解决。说起来问题其实很小但是很值得重视。看代码如下父亲类如下注意其中得equal函数
问题就出在这里
package airline.hibernate;

import java.io.Serializable;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;

import spring.mvc.bo.Selectable;


/** @author Hibernate CodeGenerator */
public class Department implements Serializable,Selectable {

    
/** identifier field */
    
private Integer departmentId;

    
/** nullable persistent field */
    
private String departmentName;

    
/** nullable persistent field */
    
private airline.hibernate.Department parent;

    
/** persistent field */
    
private List childrens;

    
/** persistent field */
    
private Set personnels=new HashSet();

    
/** full constructor */
    
public Department(Integer departmentId, String departmentName, airline.hibernate.Department parent, List childrens, Set personnels) {
        
this.departmentId = departmentId;
        
this.departmentName = departmentName;
        
this.parent = parent;
        
this.childrens = childrens;
        
this.personnels = personnels;
    }


    
/** default constructor */
    
public Department() {
        
    }


    
/** minimal constructor */
    
public Department(Integer departmentId, List childrens, Set personnels) {
        
this.departmentId = departmentId;
        
this.childrens = childrens;
        
this.personnels = personnels;
    }


    
public Integer getDepartmentId() {
        
return this.departmentId;
    }


    
public void setDepartmentId(Integer departmentId) {
        
this.departmentId = departmentId;
    }


    
public String getDepartmentName() {
        
return this.departmentName;
    }


    
public void setDepartmentName(String departmentName) {
        
this.departmentName = departmentName;
    }


    
public airline.hibernate.Department getParent() {
        
return this.parent;
    }


    
public void setParent(airline.hibernate.Department parent) {
        
this.parent = parent;
    }


    
public List getChildrens() {
        
return this.childrens;
    }


    
public void setChildrens(List childrens) {
        
this.childrens = childrens;
    }


    
public Set getPersonnels() {
        
return this.personnels;
    }


    
public void setPersonnels(Set personnels) {
        
this.personnels = personnels;
    }


    
public String toString() {
        
return new ToStringBuilder(this)
            .append(
"departmentId", getDepartmentId())
            .toString();
    }

    
    
public boolean equals(Object other) {
        
if ( (this == other ) ) return true;
        
if ( !(other instanceof Department) ) return false;
        
if(null==this.getDepartmentId())return false;
        Department castOther 
= (Department) other;
        
return new EqualsBuilder()
            .append(
this.getDepartmentId(), castOther.getDepartmentId())
            .isEquals();
    }

    
    
public int hashCode() {
        
return new HashCodeBuilder().append(getDepartmentId()).toHashCode();
    }


    
/* (non-Javadoc)
     * @see spring.mvc.bo.Selectable#getDisplayLabel(java.util.Locale)
     
*/

    
public String getDisplayLabel(Locale locale) {
        
// TODO Auto-generated method stub
        return null;
    }


    
/* (non-Javadoc)
     * @see spring.mvc.bo.Selectable#setId(java.lang.Integer)
     
*/

    
public void setId(Integer id) {
        
// TODO Auto-generated method stub
        
    }


    
/* (non-Javadoc)
     * @see spring.mvc.bo.Persistable#getId()
     
*/

    
public Integer getId() {
        
// TODO Auto-generated method stub
        return null;
    }

    
    
public void addPersonnel(Personnel personnel) {
        
if (null == getPersonnels()) {
            setPersonnels(
new java.util.HashSet());
        }


        getPersonnels().add(personnel);
    }



}

还有子类 Personnel
package airline.hibernate;

import java.io.Serializable;
import java.util.Date;
import java.util.Locale;
import java.util.Set;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;

import spring.mvc.bo.Selectable;


/** @author Hibernate CodeGenerator */
public class Personnel implements Serializable,Selectable {

    
/** identifier field */
    
private Integer personnelId;

    
/** nullable persistent field */
    
private String personnelName;

    
/** nullable persistent field */
    
private Integer personnelAge;

    
/** nullable persistent field */
    
private String personnelSex;

    
/** nullable persistent field */
    
private String personnelAddress;

    
/** nullable persistent field */
    
private String idCard;

    
/** nullable persistent field */
    
private Date personnelBirth;

    
/** nullable persistent field */
    
private Double personnelStature;

    
/** nullable persistent field */
    
private String personnelEmail;

    
/** nullable persistent field */
    
private String personnelMobilephone;

    
/** nullable persistent field */
    
private String personnelPhone;

    
/** nullable persistent field */
    
private String personnelDiploma;

    
/** nullable persistent field */
    
private Integer personnelMarriage;

    
/** nullable persistent field */
    
private String personnelHukou;

    
/** persistent field */
    
private airline.hibernate.Department department;

    
/** persistent field */
    
private Set technicaltitles;

    
/** persistent field */
    
private Set attendances;

    
/** persistent field */
    
private Set rewardspunishments;

    
/** persistent field */
    
private Set joblives;

    
/** persistent field */
    
private Set families;

    
/** persistent field */
    
private Set trains;

    
/** persistent field */
    
private Set schoolages;
    
    
private String personnelHomePhone;
    
/** full constructor */
    
private Integer departmentId;
    
public Personnel(String personnelName, Integer personnelAge, String personnelSex, String personnelAddress, String idCard, Date personnelBirth, Double personnelStature, String personnelEmail, String personnelMobilephone, String personnelPhone, String personnelDiploma, Integer personnelMarriage, String personnelHukou, airline.hibernate.Department department, Set technicaltitles, Set attendances, Set rewardspunishments, Set joblives, Set families, Set trains, Set schoolages) {
        
this.personnelName = personnelName;
        
this.personnelAge = personnelAge;
        
this.personnelSex = personnelSex;
        
this.personnelAddress = personnelAddress;
        
this.idCard = idCard;
        
this.personnelBirth = personnelBirth;
        
this.personnelStature = personnelStature;
        
this.personnelEmail = personnelEmail;
        
this.personnelMobilephone = personnelMobilephone;
        
this.personnelPhone = personnelPhone;
        
this.personnelDiploma = personnelDiploma;
        
this.personnelMarriage = personnelMarriage;
        
this.personnelHukou = personnelHukou;
        
this.department = department;
        
this.technicaltitles = technicaltitles;
        
this.attendances = attendances;
        
this.rewardspunishments = rewardspunishments;
        
this.joblives = joblives;
        
this.families = families;
        
this.trains = trains;
        
this.schoolages = schoolages;
    }


    
/** default constructor */
    
public Personnel() {
    }


    
/** minimal constructor */
    
public Personnel(airline.hibernate.Department department, Set technicaltitles, Set attendances, Set rewardspunishments, Set joblives, Set families, Set trains, Set schoolages) {
        
this.department = department;
        
this.technicaltitles = technicaltitles;
        
this.attendances = attendances;
        
this.rewardspunishments = rewardspunishments;
        
this.joblives = joblives;
        
this.families = families;
        
this.trains = trains;
        
this.schoolages = schoolages;
    }


    
public Integer getPersonnelId() {
        
return this.personnelId;
    }


    
public void setPersonnelId(Integer personnelId) {
        
this.personnelId = personnelId;
    }


    
public String getPersonnelName() {
        
return this.personnelName;
    }


    
public void setPersonnelName(String personnelName) {
        
this.personnelName = personnelName;
    }


    
public Integer getPersonnelAge() {
        
return this.personnelAge;
    }


    
public void setPersonnelAge(Integer personnelAge) {
        
this.personnelAge = personnelAge;
    }


    
public String getPersonnelSex() {
        
return this.personnelSex;
    }


    
public void setPersonnelSex(String personnelSex) {
        
this.personnelSex = personnelSex;
    }


    
public String getPersonnelAddress() {
        
return this.personnelAddress;
    }


    
public void setPersonnelAddress(String personnelAddress) {
        
this.personnelAddress = personnelAddress;
    }


    
public String getIdCard() {
        
return this.idCard;
    }


    
public void setIdCard(String idCard) {
        
this.idCard = idCard;
    }


    
public Date getPersonnelBirth() {
        
return this.personnelBirth;
    }


    
public void setPersonnelBirth(Date personnelBirth) {
        
this.personnelBirth = personnelBirth;
    }


    
public Double getPersonnelStature() {
        
return this.personnelStature;
    }


    
public void setPersonnelStature(Double personnelStature) {
        
this.personnelStature = personnelStature;
    }


    
public String getPersonnelEmail() {
        
return this.personnelEmail;
    }


    
public void setPersonnelEmail(String personnelEmail) {
        
this.personnelEmail = personnelEmail;
    }


    
public String getPersonnelMobilephone() {
        
return this.personnelMobilephone;
    }


    
public void setPersonnelMobilephone(String personnelMobilephone) {
        
this.personnelMobilephone = personnelMobilephone;
    }


    
public String getPersonnelPhone() {
        
return this.personnelPhone;
    }


    
public void setPersonnelPhone(String personnelPhone) {
        
this.personnelPhone = personnelPhone;
    }


    
public String getPersonnelDiploma() {
        
return this.personnelDiploma;
    }


    
public void setPersonnelDiploma(String personnelDiploma) {
        
this.personnelDiploma = personnelDiploma;
    }


    
public Integer getPersonnelMarriage() {
        
return this.personnelMarriage;
    }


    
public void setPersonnelMarriage(Integer personnelMarriage) {
        
this.personnelMarriage = personnelMarriage;
    }


    
public String getPersonnelHukou() {
        
return this.personnelHukou;
    }


    
public void setPersonnelHukou(String personnelHukou) {
        
this.personnelHukou = personnelHukou;
    }


    
public airline.hibernate.Department getDepartment() {
        
return this.department;
    }


    
public void setDepartment(airline.hibernate.Department department) {
        
this.department = department;
    }


    
public Set getTechnicaltitles() {
        
return this.technicaltitles;
    }


    
public void setTechnicaltitles(Set technicaltitles) {
        
this.technicaltitles = technicaltitles;
    }


    
public Set getAttendances() {
        
return this.attendances;
    }


    
public void setAttendances(Set attendances) {
        
this.attendances = attendances;
    }


    
public Set getRewardspunishments() {
        
return this.rewardspunishments;
    }


    
public void setRewardspunishments(Set rewardspunishments) {
        
this.rewardspunishments = rewardspunishments;
    }


    
public Set getJoblives() {
        
return this.joblives;
    }


    
public void setJoblives(Set joblives) {
        
this.joblives = joblives;
    }


    
public Set getFamilies() {
        
return this.families;
    }


    
public void setFamilies(Set families) {
        
this.families = families;
    }


    
public Set getTrains() {
        
return this.trains;
    }


    
public void setTrains(Set trains) {
        
this.trains = trains;
    }


    
public Set getSchoolages() {
        
return this.schoolages;
    }


    
public void setSchoolages(Set schoolages) {
        
this.schoolages = schoolages;
    }


      
public String toString() {
        
return new ToStringBuilder(this).append("personnelIdCard", getIdCard())
                                        .toString();
    }


    
public boolean equals(Object other) {
        
if ((this == other)) {
            
return true;
        }


        
if (!(other instanceof Personnel)) {
            
return false;
        }


        Personnel castOther 
= (Personnel) other;
        
if(null==getId())return false;
        
return new EqualsBuilder().append(this.getIdCard(),
            castOther.getIdCard()).isEquals();
    }


    
public int hashCode() {
        
return new HashCodeBuilder().append(getIdCard()).toHashCode();
    }


    
/* (non-Javadoc)
     * @see spring.mvc.bo.Selectable#getDisplayLabel(java.util.Locale)
     
*/

    
public String getDisplayLabel(Locale locale) {
        
// TODO Auto-generated method stub
        return null;
    }


    
/* (non-Javadoc)
     * @see spring.mvc.bo.Selectable#setId(java.lang.Integer)
     
*/

    
public void setId(Integer id) {
        
// TODO Auto-generated method stub
        
    }


    
/* (non-Javadoc)
     * @see spring.mvc.bo.Persistable#getId()
     
*/

    
public Integer getId() {
        
// TODO Auto-generated method stub
        return null;
    }



    
/**
     * @return Returns the personnelHomePhone.
     
*/

    
public String getPersonnelHomePhone() {
        
return personnelHomePhone;
    }

    
/**
     * @param personnelHomePhone The personnelHomePhone to set.
     
*/

    
public void setPersonnelHomePhone(String personnelHomePhone) {
        
this.personnelHomePhone = personnelHomePhone;
    }

    
/**
     * @return Returns the departmentId.
     
*/

    
public Integer getDepartmentId() {
        
return departmentId;
    }

    
/**
     * @param departmentId The departmentId to set.
     
*/

    
public void setDepartmentId(Integer departmentId) {
        
this.departmentId = departmentId;
    }

}

也要注意其中得equal 原来时没有这句得if(null==getId())return false; 问题就出在这里在没有这句时候 hibernate
在做inster得操作时 先对 两个pojo判等因为 没有插入之前 id为null 而没有if(null==getId())return false; 得时候
即使id为null 两个pojo也会判定为相等得所有只插入第一条。所以说在hibernate pojo中equal是很重要得