Categories
Mastering Development

How to avoid updation of Table A (lsa_line_of_business) while updating Table B (lsa_skill) alone using saveOrUpdate()?

My application’s database has two master tables. lsa_line_of_business & lsa_skill . Both tables are populated by insert statements execution in PostgreSQL Database (9.6.12) . But, We have option to Update one skill at a time (updation of single record in lsa_skill table) from UI. When I do so, it is also updating lsa_line_of_business table also which shouldn’t happen. Please assist how can I avoid updation of lsa_line_of_business table ?

‘lsa_line_of_business’ definition :

  CREATE TABLE public.lsa_line_of_business
  (
     line_of_business_id integer NOT NULL,
     line_of_business_name character varying,
    line_of_business_desc character varying,
    lob_hierarchy integer,
     reason_code integer,
   CONSTRAINT lsa_line_of_business_pkey PRIMARY KEY (line_of_business_id)
   )
     WITH (
  OIDS=FALSE
);
ALTER TABLE public.lsa_line_of_business
 OWNER TO lsasdev;

‘lsa_skill’ definition

 CREATE TABLE public.lsa_skill
    (
  skill_id numeric NOT NULL DEFAULT nextval('lsa_skill_skill_id_seq'::regclass),
  skill_name character varying(100),
  skill_desc character varying,
  skill_status character varying(100),
  created_by character varying(100),
   created_date timestamp without time zone,
   updated_date timestamp without time zone,
    updated_by character varying(100),
  skill_order integer,
  assignment_priority character varying,
  line_of_business_id integer,
  hhf_state_specific_skill boolean,
   CONSTRAINT lsa_skill_pkey PRIMARY KEY (skill_id)
 )
WITH (
 OIDS=FALSE
 ); ALTER TABLE public.lsa_skill
 OWNER TO lsasdev;

LineOfBusinessHibVO.java class

           import javax.persistence.Column;
           import javax.persistence.Entity;
           import javax.persistence.GeneratedValue;
           import javax.persistence.GenerationType;
           import javax.persistence.Id;
           import javax.persistence.Inheritance;
           import javax.persistence.InheritanceType;
        import javax.persistence.Table;


    @Entity
   @Table(name = "lsa_line_of_business")
  @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
  public class LineOfBusinessHibVO {

public LineOfBusinessHibVO() {

}

@Id
@Column(name = "line_of_business_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer lineOfBusinessId;

@Column(name = "line_of_business_name")
private String lineOfBusinessName;

@Column(name = "line_of_business_desc")
private String lineOfBusinessDesc;

@Column(name="reason_code")
private Integer reasonCode;



 // getters & setters
 
  }

SkillHibVO.java class

   import java.io.Serializable;
   import javax.persistence.CascadeType;
  import javax.persistence.Column;
 import javax.persistence.Entity;
 import javax.persistence.Id;
 import javax.persistence.Inheritance;
 import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;

import org.hibernate.annotations.Parameter;
import org.hibernate.annotations.Type;
import org.joda.time.DateTime;

   @Entity
  @Table(name = "lsa_skill")
  @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
  public class SkillHibVO implements Serializable {
  private static final long serialVersionUID = 1L;

@Id
@Column(name="skill_id")
private Integer skillId;
@Column(name="skill_name")
private String skillName;
@Column(name="skill_desc")
private String skillDesc;
@Column(name="skill_status")
private String skillStatus;
@Column(name="created_by")
private String createdBy;
@Column(name="updated_by")
private String updatedBy;

@Type(type="org.jadira.usertype.dateandtime.joda.PersistentDateTime")
@Column(name="created_date")
private DateTime createdDate;

 @Type(type="org.jadira.usertype.dateandtime.joda.PersistentDateTime",
            parameters = { @Parameter(name = "databaseZone", value = "PST8PDT"), @Parameter(name = "javaZone", value = "jvm")})
@Column(name="updated_date")
private DateTime updatedDate;
 
@Column(name="skill_order")
private Integer skillOrder;
@OneToOne(cascade = CascadeType.ALL )
@JoinColumn(name = "line_of_business_id", referencedColumnName="line_of_business_id" , 
insertable=false ,updatable=false)
private LineOfBusinessHibVO lineOfBusiness;
@Column(name = "assignment_priority")
private String assignmentPriority;

public Integer getSkillId() {
    return skillId;
}


public void setSkillId(Integer skillId) {
    this.skillId = skillId;
}


public String getSkillName() {
    return skillName;
}


public void setSkillName(String skillName) {
    this.skillName = skillName;
}


public String getSkillDesc() {
    return skillDesc;
}

public void setSkillDesc(String skillDesc) {
    this.skillDesc = skillDesc;
}


public String getSkillStatus() {
    return skillStatus;
}


public void setSkillStatus(String skillStatus) {
    this.skillStatus = skillStatus;
}


public DateTime getCreatedDate() {
    return createdDate;
}


public void setCreatedDate(DateTime createdDate) {
    this.createdDate = createdDate;
}
public DateTime getUpdatedDate() {
    return updatedDate;
}
public void setUpdatedDate(DateTime updatedDate) {
    this.updatedDate = updatedDate;
}

public Integer getSkillOrder() {
    return skillOrder;
}

public void setSkillOrder(Integer skillOrder) {
    this.skillOrder = skillOrder;
}

public LineOfBusinessHibVO getLineOfBusiness() {
    return lineOfBusiness;
}

/**
 * @param lineOfBusinessHibVO the lineOfBusinessHibVO to set
 */
public void setLineOfBusiness(LineOfBusinessHibVO lineOfBusiness) {
    this.lineOfBusiness = lineOfBusiness;
}


public String getAssignmentPriority() {
    return assignmentPriority;
}

public void setAssignmentPriority(String assignmentPriority) {
    this.assignmentPriority = assignmentPriority;
}

/**
 * @return the createdBy
 */
public String getCreatedBy() {
    return createdBy;
}

public void setCreatedBy(String createdBy) {
    this.createdBy = createdBy;
}


public String getUpdatedBy() {
    return updatedBy;
}


public void setUpdatedBy(String updatedBy) {
    this.updatedBy = updatedBy;
}

}

DAO Layer method to update SkillHibVO :

  @Override
   public void saveOrUpdate(SkillHibVO skillHibVO) {
      log.debug("Entered SkillHibDAO : saveOrUpdate");
      Session session = sessionFactory.getCurrentSession();
      session.saveOrUpdate(skillHibVO);
      log.debug("Exit SkillHibDAO : saveOrUpdate");
 }

This code is updating LineOfBusinessHibVO record (PFA image at top) with the line_of_business_id value using SkillHibVO record which shouldn’t happen. Let me know what changes needs to be done. Please assist.

Leave a Reply

Your email address will not be published. Required fields are marked *