2 Replies Latest reply on Dec 1, 2008 11:38 PM by Christian Bauer

    Please help, removing child entity from parent Home object.

    Val Sw Expert
      I have entities with realtionship like :
      -Project entity can have multiple QuantExperiments.
      -QuantExperiment entity can have multiple Reagents and
      -Reagent entity can have multiple Treatments.

      We have a functionality where user clicks a link to remove reagent from QuantExperimentEdit.xhtml.
      Clicking on "Remove Reagent" removes particular row from ui.

      But when I click "Update Experiment", it removes ALL treatments, ALL reagents, Experiments and project.

      I am overriding QuantExperimentHome.update(), where I am checking the reagent and treatments which needs to be removed.

      QuantExperimentEdit.xhtml:
      -------------------------
      <s:link action="#{quantExperimentHome.removeReagentLink}" 
                value="Remove Reagent">
           <f:param name="hashcode" value="#{info.hashCode()}"/>                                                 
      </s:link>     

      <h:commandButton id="update"
        value="Update Experiment"
      action="#{quantExperimentHome.update}"
      rendered="#{quantExperimentHome.managed}"/>

      QuantExperimentHome.java
      ------------------------
      public String removeReagentLink(){          
           javax.faces.context.FacesContext facesContext = (new org.jboss.seam.faces.FacesContext()).getContext();
           javax.servlet.http.HttpServletRequest hsr=(javax.servlet.http.HttpServletRequest)facesContext.getCurrentInstance().getExternalContext().getRequest();
           String reagentHashcode=hsr.getParameter("hashcode");
           
           Reagent[] reagentNew=null;
           
           if(reagent1==null || reagent1.length==1){
                //Added for "At least one reagent is required to create a Project" validation
                return "fail";
           }else{
                reagentNew=new Reagent[reagent1.length-1];
           }
           int j=0;
           for(int i=0;i<reagentNew.length;i++){
                   if(reagent1[j].hashCode()!=Integer.parseInt(reagentHashcode)){
                          reagentNew[i]=reagent1[j];
                          j++;                      
                   }else{
                        i--;
                        j++;                      
                   }
           }
           this.reagent1=reagentNew;
           return "";          
      }

      @Override
      public String update(){          
           QuantExperiment qeUpdate = getInstance();     

           List<Reagent> re=getInstance().getReagent();

           int xs=re.size();
           for(int i=0;i<xs;i++){
                Reagent reg=re.get(i);
                if(!findMatchingReagent(reg)){
                     System.out.println(reg.getPname()+" has been removed.");
                     List<Treatment> trs=reg.getTreatment();
                     int xt=trs.size();
                     for(int k=0;k<xt;k++){                    
                          entityManager.remove(trs.get(k));                         
                     }               
                     entityManager.remove(reg);          
                }else{                    
                     System.out.println(reg.getHjid()+"  has not been removed");
                }
           }          
           re.clear();
           
           for(int i=0;i<this.reagent1.length;i++){
                re.add(reagent1[i]);
           }
           return super.update();
      }

      private boolean findMatchingReagent(Reagent re){
           boolean bl=false;
           for(int j=0;j<this.reagent1.length;j++){
                if(reagent1[j].getHjid()==re.getHjid()){
                     bl=true;
                     break;
                }
           }
           return bl;
      }
        • 1. Re: Please help, removing child entity from parent Home object.
          Val Sw Expert
          Adding to above details there are only 4 entities related as follows :

          I started from scratch i.e. generated seam project using "seam generate-ui" command and created some records for each relational entity.

          -Project entity can have multiple QuantExperiments.
          -QuantExperiment entity can have multiple Reagents and
          -Reagent entity can have multiple Treatments.

          I created few Treatments for Reagents and few reagents for experiments.

          When I try to delete only ONE treatment it deletes ALL treatments, ALL reagents, etc...

          Please suggest what is wrong, is there any issue with entities.

          Project entity :
          ----------------
          public class Project
              implements Equals, HashCode, ToString{
          ......
          @OneToMany(fetch=FetchType.EAGER)
          protected List<QuantExperiment> quantExperiment;

          @OneToMany(cascade = {CascadeType.ALL}, mappedBy="project")
          public List<QuantExperiment> getQuantExperiment() {
               if (quantExperiment == null) {
                    quantExperiment = new ArrayList<QuantExperiment>();
               }
               return this.quantExperiment;
          }

          QuantExperiment entity :
          ------------------------
          public class QuantExperiment
              implements Equals, HashCode, ToString {
          ......
          protected List<Reagent> reagent;
          protected Project project;

          @ManyToOne(cascade = {CascadeType.ALL},optional=false)
          public Project getProject() {
               return project;
          }

          @OrderBy
          @OneToMany(cascade = {CascadeType.ALL}, mappedBy="quantExperiment")
          public List<Reagent> getReagent() {
               if (reagent == null) {
                    reagent = new ArrayList<Reagent>();
               }
               return this.reagent;
          }

          Reagent entity :
          ----------------
          public class Reagent
              implements Equals, HashCode, ToString {
          ....
          protected List<Treatment> treatment;
          protected QuantExperiment quantExperiment;

          @ManyToOne(cascade = {CascadeType.ALL},optional=false)
          public QuantExperiment getQuantExperiment() {
               return quantExperiment;
          }

          @OrderBy
          @OneToMany(cascade = {CascadeType.ALL}, mappedBy="reagent")     
          public List<Treatment> getTreatment() {
               if (treatment == null) {
                    treatment = new ArrayList<Treatment>();
               }
               return this.treatment;
          }

          Treatment entity :
          ------------------
          public class Treatment
              implements Equals, HashCode, ToString {
          ......
          protected Reagent reagent;
          @ManyToOne(cascade = {CascadeType.ALL},optional=false)
          public Reagent getReagent() {
               return reagent;
          }
          ....
          • 2. Re: Please help, removing child entity from parent Home object.
            Christian Bauer Master

            Stop cross-posting! As you have already been told, most of your questions do not belong here, ask them on the Hibernate forum. If you post on the Hibernate forum, do not post here again.