0

The problem is that when a person's Doctorate school information changes, other school information also changes. İt has degree title, school and department values. For example, the Master's, Bachelor's, Associate's and high school information cannot be changed through the web interface until the doctorate values are changed. When Doctorate values changed, Master's, Bachelor's, Associate's and high school values changed in the same way as Doctorate values on website.

I am new at Java and can't figure Hibernate or lazy model caused it.

public boolean merge(EmployeeEducationInfo obj) {
    this.highSchool.merge(obj.getHighSchool());
    this.associate.merge(obj.getAssociate());
    this.bachelor.merge(obj.getBachelor());
    this.master.merge(obj.getMaster());
    this.doctoral.merge(obj.getDoctoral());

    if(this.getEducationStatus().getId().compareTo(obj.getEducationStatus().getId()) != 0) {
         this.setEducationStatus(obj.getEducationStatus());
    }
    if(this.getShortDescription().compareTo(obj.getShortDescription()) != 0) {
         this.setShortDescription(obj.getShortDescription());
    }
    if(this.getDescription().compareTo(obj.getDescription()) != 0) {
         this.setDescription(obj.getDescription());
    }
    
    return true;
}

When I comment this line this.doctoral.merge(obj.getDoctoral()); and enter new values on web site, Master's values now replace license and other values. When I comment the Master's //this.master.merge(obj.getMaster());, Bachelor's, Associate's and high school lines respectively by one by, all the values are corrected when I get rid of the comment line. but it only happens for one person, the same error is repeated for a new person on the website.

@With
@Getter
@AllArgsConstructor
@Table(name = "----", schema = "---")
@Entity(name = "EmployeeEducationInfo")
@EqualsAndHashCode(callSuper = false)
public class EmployeeEducationInfo extends BaseEntity {

    private static final long serialVersionUID = -1644326415323600803L;

    @NonNull
    @Id
    @Column(name = "ID", unique = true)
    @GeneratedValue(generator = "employeeEducationInfoSequence")
    @GenericGenerator(name = "employeeEducationInfoSequence", strategy = "org.hibernate.id.enhanced.SequenceStyleGenerator", parameters = {
            @Parameter(name = "---", value = "---"),
            @Parameter(name = "---", value = "1000"), @Parameter(name = "---", value = "1"),
            @Parameter(name = "---", value = "---") })
    private Integer id;

    public EmployeeEducationInfo() {
        description = " ";
        shortDescription = " ";
    }

    public void setHighSchool(  EmployeeDegreeInfo newhighSchool) {
        this. highSchool = newhighSchool;
    }

    @NonNull
    @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinColumn(name = "HIGH_SCHOOL_DEGREE_ID")
    private EmployeeDegreeInfo highSchool;
    
    public void setAssociate(EmployeeDegreeInfo newAssociate) {
        this.associate = newAssociate;
    }

    @NonNull
    @OneToOne(targetEntity = EmployeeDegreeInfo.class, cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinColumn(name = "ASSOCIATE_DEGREE_ID")
    private EmployeeDegreeInfo associate;
    
    public void setBachelor(EmployeeDegreeInfo newBachelor) {
        this.bachelor = newBachelor;
    }

    @NonNull
    @OneToOne(targetEntity = EmployeeDegreeInfo.class, cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinColumn(name = "BACHELOR_DEGREE_ID", nullable = false, unique = true)
    private EmployeeDegreeInfo bachelor;
    
    public void setMaster(EmployeeDegreeInfo newMaster) {
        this.master = newMaster;
    }

    @NonNull
    @OneToOne(targetEntity = EmployeeDegreeInfo.class, cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinColumn(name = "MASTER_DEGREE_ID", nullable = false, unique = true)
    private EmployeeDegreeInfo master;

    public void setDoctoral(EmployeeDegreeInfo newDoctoral) {
        this.doctoral = newDoctoral;
    }

    @NonNull
    @OneToOne(targetEntity = EmployeeDegreeInfo.class, cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinColumn(name = "DOCTORAL_DEGREE_ID", nullable = false, unique = true)
    private EmployeeDegreeInfo doctoral;

    public void setEducationStatus(EducationStatus newEducationStatus) {
        this.educationStatus = newEducationStatus;
    }

    @NonNull
    @OneToOne(targetEntity = EducationStatus.class, fetch = FetchType.EAGER)
    @JoinColumn(name = "EDUCATION_STATUS_ID", nullable = false)
    private EducationStatus educationStatus;

    public int compareTo(EmployeeEducationInfo other) {
        return this.description.compareTo(other.getDescription());
    }

    public EmployeeEducationInfo clone() {
        EmployeeEducationInfo clone = withId(getId());

        if (getHighSchool() != null) {
            clone.setHighSchool(getHighSchool());
        }

        if (getAssociate() != null) {
            clone.setAssociate(getAssociate());
        }

        if (getBachelor() != null) {
            clone.setBachelor(getBachelor());
        }

        if (getMaster() != null) {
            clone.setMaster(getMaster());
        }

        if (getDoctoral() != null) {
            clone.setDoctoral(getDoctoral());
        }

        if (getEducationStatus() != null) {
            clone.setEducationStatus(getEducationStatus());
        }

        return clone;
    }
    
    public boolean haveDifference(EmployeeEducationInfo obj) {
        return !(this.id.compareTo(obj.getId()) == 0
                && this.educationStatus.getId().compareTo(obj.getEducationStatus().getId()) == 0
                && this.shortDescription.compareTo(obj.getShortDescription()) == 0
                && this.description.compareTo(obj.getDescription()) == 0);
    }
    
    public boolean merge(EmployeeEducationInfo obj) {
//      if(!haveDifference(obj)) {
//          return false;
//      }
        
        this.highSchool.merge(obj.getHighSchool());
        this.associate.merge(obj.getAssociate());
        this.bachelor.merge(obj.getBachelor());
        this.master.merge(obj.getMaster());
        this.doctoral.merge(obj.getDoctoral());
        
        if(this.getEducationStatus().getId().compareTo(obj.getEducationStatus().getId()) != 0) {
             this.setEducationStatus(obj.getEducationStatus());
        }
        if(this.getShortDescription().compareTo(obj.getShortDescription()) != 0) {
             this.setShortDescription(obj.getShortDescription());
        }
        if(this.getDescription().compareTo(obj.getDescription()) != 0) {
             this.setDescription(obj.getDescription());
        }
        
        return true;
    }
}
@With
@Getter 
@AllArgsConstructor
@Table(name = "---", schema = "---")
@Entity(name = "-----")
@EqualsAndHashCode(callSuper = false)
public class EmployeeDegreeInfo extends BaseEntity {
    private static final long serialVersionUID = -1644326415323600803L;

    @NonNull
    @Id
    @Column(name = "ID", unique = true)
    @GenericGenerator(name = "employeeDegreeInfoSequence", strategy = "org.hibernate.id.enhanced.SequenceStyleGenerator", parameters = {
        @Parameter(name = "---", value = "----"),
        @Parameter(name = "---", value = "1000"),
        @Parameter(name = "-----", value = "1"),
        @Parameter(name = "-----", value = "----")
    })
    @GeneratedValue(generator = "employeeDegreeInfoSequence")
    private Integer id;
    
    public EmployeeDegreeInfo() {
        description      = " ";
        shortDescription = " ";
    }
    
    public void setGraduationDate(Date newGraduationDate) {
        this.graduationDate = newGraduationDate;
    }

    @Temporal(TemporalType.DATE)
    @Column(name = "GRADUATION_DATE")
    private Date graduationDate;
    
    public void setDegree( Degree newDegree) {
        this.degree = newDegree;
    }

    @NonNull
    @ManyToOne(optional = false, fetch = FetchType.EAGER)
    @JoinColumn(name = "DEGREE_ID", nullable = false)
    private Degree degree;
    
    public void setDegreeTitle(DegreeTitle newDegreeTitle) {
        this.degreeTitle = newDegreeTitle;
    }

    @NonNull
    @ManyToOne(optional = false, fetch = FetchType.EAGER)
    @JoinColumn(name = "DEGREE_TITLE_ID", nullable = false)
    private DegreeTitle degreeTitle;
    
    public void setSchool(School newSchool) {
        this.school = newSchool;
    }

    @NonNull
    @ManyToOne(optional = false, fetch = FetchType.EAGER)
    @JoinColumn(name = "SCHOOL_ID")
    private School school;
    
    public void setEducationStatus(EducationStatus neweducationStatus) {
        this.educationStatus = neweducationStatus;
    }

    @NonNull
    @ManyToOne(optional = false, fetch = FetchType.EAGER)
    @JoinColumn(name = "EDUCATION_STATUS_ID")
    private EducationStatus educationStatus;
    
    public int compareTo(EmployeeDegreeInfo other) {
        return this.description.compareTo(other.getDescription());
    }
    
    public EmployeeDegreeInfo clone() {
        EmployeeDegreeInfo clone = withId(getId());
        
        if(getDegree() != null) {
            clone.setDegree(getDegree());
        }
        
        if(getSchool() != null) {
            clone.setSchool(getSchool());
        }
        
        if(getEducationStatus() != null) {
            clone.setEducationStatus(getEducationStatus().clone());
        }
        
        return clone;
    }
    
    public boolean merge(EmployeeDegreeInfo obj) {
        if(compareDatesWithNullControl(this.graduationDate, obj.getGraduationDate()) != 0) {
             this.setGraduationDate(obj.getGraduationDate());
        }
        if(this.getDegree().getId().compareTo(obj.getDegree().getId()) != 0) {
             this.setDegree(obj.getDegree());
        }
        if(this.getDegreeTitle().getId().compareTo(obj.getDegreeTitle().getId()) != 0) {
             this.setDegreeTitle(obj.getDegreeTitle());
        }
        if(this.getSchool().getId().compareTo(obj.getSchool().getId()) != 0) {
             this.setSchool(obj.getSchool());
        }
        if(this.getEducationStatus().getId().compareTo(obj.getEducationStatus().getId()) != 0) {
             this.setEducationStatus(obj.getEducationStatus());
        }
        if(this.getShortDescription().compareTo(obj.getShortDescription()) != 0) {
             this.setShortDescription(obj.getShortDescription());
        }
        if(this.getDescription().compareTo(obj.getDescription()) != 0) {
             this.setDescription(obj.getDescription());
        }
        
        return true;
    }
}

On these pages I tried to code setters instead of lazy model. İt doesn't work.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197

0 Answers0