3 Replies Latest reply on Nov 28, 2008 6:30 PM by Val Sw

    Help - javax.persistence.PersistenceException: org.hibernate.PropertyValueException: not-null property references a null or transient value

    Val Sw Expert
      I have a function which adds fields of an entity, when I try to add on update screen it gives the following error, same thing works fine on create screen.

      Error message:
      --------------------------------------
      Hibernate:
          select
              MET_TREATMENT_SEQ.nextval
          from
              dual
      16:11:54,908 WARN  [arjLoggerI18N] [com.arjuna.ats.arjuna.coordinator.TwoPhaseCoordinator_2] TwoPhaseCoordinator.beforeCompletion - failed for com.arjuna.ats.internal.jta.resources.arjunacore.SynchronizationImple@1abb373
      javax.persistence.PersistenceException: org.hibernate.PropertyValueException: not-null property references a null or transient value: com.entity.Treatment.reagent
           at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:629)
      -------------------------------------

      a) This link calls quantExperimentHome.addTreatmentLink
      <h:commandLink action="#{quantExperimentHome.addTreatmentLink}" 
           value="Add treatments" immediate="true">
           <f:param name="re_hashcode" value="#{info.hashCode()}"/>                                                 
      </h:commandLink>

      quantExperimentHome.addTreatmentLink method
      -------------------------------------------
      public String addTreatmentLink(){
           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("re_hashcode");
           for(int i=0;i<reagent1.length;i++){
                   if(reagent1[i].hashCode()==Integer.parseInt(reagentHashcode)){
                          reagent1[i].getTreatment().add(new Treatment());
                     break;
                   }
           }
           return "added";     
      }

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

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

      public void setTreatment(List<Treatment> treatment) {
           for(Treatment t : treatment){
                t.setReagent(this);
           }
           this.treatment = treatment;
      }
      @ManyToOne(optional=false)
      public QuantExperiment getQuantExperiment() {
           return quantExperiment;
      }

      Treatment entity
      ----------------
      public class Treatment
          implements Equals, HashCode, ToString
      {
      protected Reagent reagent;

      @ManyToOne(optional=false)
      public Reagent getReagent() {
           return reagent;
      }
      public void setReagent(Reagent reagent) {
           this.reagent = reagent;
      }
        • 1. Re: Help - javax.persistence.PersistenceException: org.hibernate.PropertyValueException: not-null property references a null or transient value
          Val Sw Expert
          Please help on this....

          I have a function (QuantExperimentHome.addTreatmentLink()) which adds fields of an treatment entity. When I try to add on update screen it adds a row on UI but calls hibernate and inserts a row, same thing works fine on create screen.

          Issue : "It calls hibernate and enters record in treatment table... it should only add row to ui... and should not do any insert..."
          I have also attached QuantExperimentHome code, Reagent and Treatment entity..

          Log on clicking Add treatment link:
          --------------------------
          [STDOUT] QuantExperimentHome.addTreatmentLink(), reagent[1]937235108, HASCODE FROM FORM=937235108
          [STDOUT] QuantExperimentHome.addTreatmentLink() 1, reagent[1].treatment.lenth2
          [STDOUT] QuantExperimentHome.addTreatmentLink() 2, reagent[1].treatment.lenth3
          [STDOUT] Hibernate:
              select
                  MET_TREATMENT_SEQ.nextval
              from
                  dual
          [STDOUT] Hibernate:
              insert
              into
                  MET_TREATMENT
                  (NOTE, LASTUPDATEDTIMESTAMP, reagent_HJID, TREATMENTCONCENTRATION, TREATMENTCONCENTRATIONUNIT, TREATMENTCOMPOUNDNAME, TREATMENTTIMEUNIT, TREATMENTDURATION, HJID)
              values
                  (?, ?, ?, ?, ?, ?, ?, ?, ?)
          ---------------------------------

          Is it because on update screen, reagent is managed that is why it calls hibernate and inserts record on clicking "Add treatment", how can I handle this

          Reagent entity contains treatment and 1 reagent can have multiple treatments

          XHTML:
          ------
          <ui:repeat value="#{quantExperimentHome.reagent1}" var="info" >
          <h:inputText id="pname1" required="true" value="#{info.pname}"/>
             ....
          <ui:repeat value="#{info.treatment}" var="itreat">   
          <h:inputText id="treatmentCompoundName" value="#{itreat.treatmentCompoundName}"/>
                   ......
          </ui:repeat>   
          <h:commandLink action="#{quantExperimentHome.addTreatmentLink}" 
             value="Add treatments" immediate="true" >
          <f:param name="re_hashcode" value="#{info.hashCode()}"/>                                                      </h:commandLink>
          </ui:repeat>

          QuantExperimentHome.addTreatment() method
          ------------------------------------------
            public String addTreatmentLink(){
          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("re_hashcode");
          for(int i=0;i<reagent1.length;i++){
          System.out.println("QuantExperimentHome.addTreatmentLink(), reagent["+i+"]"+reagent1[i].hashCode()+", HASCODE FROM FORM="+reagentHashcode);
          if(reagent1[i].hashCode()==Integer.parseInt(reagentHashcode)){
          System.out.println("QuantExperimentHome.addTreatmentLink() 1, reagent["+i+"].treatment.lenth"+reagent1[i].getTreatment().size());
             /////////////
             Treatment treatment = new Treatment();
             treatment.setReagent(reagent1[i]);
             reagent1[i].getTreatment().add(treatment);
             ///////////
             //reagent1[i].getTreatment().add(new Treatment());
          System.out.println("QuantExperimentHome.addTreatmentLink() 2, reagent["+i+"].treatment.lenth"+reagent1[i].getTreatment().size());
          break;
          }
          }
          return "";   
          }   
          -----------------------------------------
          @Name("quantExperimentHome")
          public class QuantExperimentHome extends EntityHome<QuantExperiment> {

             @In(create = true)
             ProjectHome projectHome;
             
             @Out(required=false)
             @In(required=false)
             TreatmentHome treatmentHome;


             public void setQuantExperimentHjid(Long id) {
                setId(id);
             }

             public Long getQuantExperimentHjid() {
                return (Long) getId();
             }

             @Override
             protected QuantExperiment createInstance() {
                QuantExperiment quantExperiment = new QuantExperiment();
                return quantExperiment;
             }

             public void wire() {
                getInstance();
                Project project = projectHome.getDefinedInstance();
                if (project != null) {
                   getInstance().setProject(project);
                }
             }

             public boolean isWired() {
                if (getInstance().getProject() == null)
                   return false;
                return true;
             }

             public QuantExperiment getDefinedInstance() {
                return isIdDefined() ? getInstance() : null;
             }

             public List<Reagent> getReagent() {
                return getInstance() == null ? null : new ArrayList<Reagent>(
                      getInstance().getReagent());
             }
             
             //CUSTOM FUNCTIONALITY STARTS here
             @Out(required=false)
             @In(required=false)   
              ReagentHome reagentHome=new ReagentHome();
             
             @In(create=true)   
              private FacesMessages facesMessages;

             @In
             private EntityManager entityManager;

              private Reagent[] reagent1=new Reagent[]{new Reagent()};
             
             private Reagent initializeReagent(Reagent re){
                List<Treatment> t=re.getTreatment();
                if(t.size()<1){
                   t.add(new Treatment());         
                }
                re.setTreatment(t);
                return re;
             }
             
              public QuantExperimentHome(){
             super();
             Identity.setSecurityEnabled(true);
             for(int i=0;i<reagent1.length;i++){
             reagent1[i]=initializeReagent(reagent1[i]);
             }
             }

                
            public String addTreatmentLink(){
          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("re_hashcode");
          for(int i=0;i<reagent1.length;i++){
          System.out.println("QuantExperimentHome.addTreatmentLink(), reagent["+i+"]"+reagent1[i].hashCode()+", HASCODE FROM FORM="+reagentHashcode);
          if(reagent1[i].hashCode()==Integer.parseInt(reagentHashcode)){
          System.out.println("QuantExperimentHome.addTreatmentLink() 1, reagent["+i+"].treatment.lenth"+reagent1[i].getTreatment().size());
             /////////////
             Treatment treatment = new Treatment();
             treatment.setReagent(reagent1[i]);
             reagent1[i].getTreatment().add(treatment);
             ///////////
             //reagent1[i].getTreatment().add(new Treatment());
          System.out.println("QuantExperimentHome.addTreatmentLink() 2, reagent["+i+"].treatment.lenth"+reagent1[i].getTreatment().size());
          break;
          }
          }
          return "";   
          }   
             
          public String persist(){       
          java.util.ArrayList<QuantExperiment> arrListQuantExp=new java.util.ArrayList<QuantExperiment>();
          QuantExperiment qe=getInstance();      
          ArrayList<Reagent> arrListReagent=new ArrayList<Reagent>();            
          for(int i=0;i<reagent1.length;i++){      
          arrListReagent.add(reagent1[i]);   
          }
                
          super.persist();
          return "";
          }
             
            public void editProject(){
              List<Reagent> liRe=getInstance().getReagent();
             reagent1 = new Reagent[liRe.size()];
                
             for(int i=0;i<reagent1.length;i++){
             reagent1[i]=liRe.get(i);
             ReagentHome rh=new ReagentHome();
             rh.setInstance(reagent1[i]);
             rh.treatmentHome=new TreatmentHome();
             rh.treatmentHome.setTreatmentHjid(reagent1[i].getHjid());
             //rh.wire();
             }
            }
             
            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;
          }
             
              @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)){
                      List<Treatment> trs=reg.getTreatment();
                      int xt=trs.size();
                      for(int k=0;k<xt;k++){
                         entityManager.remove(trs.get(k));
                      }
                      entityManager.remove(reg);
                      //rh.remove();
                   }else{
                      System.out.println(reg.getHjid()+"  has not been removed");
                   }
                }
                re.clear();
               
                for(int i=0;i<this.reagent1.length;i++){
                   reagent1[i].setLabel(qeUpdate.getQuantitationType());
                   reagent1[i].setEntryPoint(reagent1[i].getAffinityCompound());
                   re.add(reagent1[i]);
                }
                return super.update();
              }
             
             /**
              * @return the reagent1
              */
             public Reagent[] getReagent1() {
                return reagent1;
             }

             /**
              * @param reagent1 the reagent1 to set
              */
             public void setReagent1(Reagent[] reagent1) {
                this.reagent1 = reagent1;
             }
          }


          Reagent Entity
          ----------------
          protected List<Treatment> treatment;
          @OneToMany(cascade = {CascadeType.ALL}, mappedBy="reagent")
          public List<Treatment> getTreatment() {
                  if (treatment == null) {
                      treatment = new ArrayList<Treatment>();
                  }
                  return this.treatment;
              }
          public void setTreatment(List<Treatment> treatment) {
                    for(Treatment t : treatment){
                       t.setReagent(this);
                    }
                     this.treatment = treatment}

          Treatment entity
          -------------------
          public class Treatment impl...{
          protected Reagent reagent;
          @ManyToOne(optional=false)
             public Reagent getReagent() {
                return reagent;
             }
          public void setReagent(Reagent reagent) {
                this.reagent = reagent;
             }
          • 3. Re: Help - javax.persistence.PersistenceException: org.hibernate.PropertyValueException: not-null property references a null or transient value
            Val Sw Expert
            Thanks Stuart, I think I am close to it, I added "@ManyToOne(cascade = {CascadeType.PERSIST},optional=false)" to Treatment.getReagent, Updating treatment works fine....

            I need help to on update, I am new to this .. :(, I would really appreciate your help...

            When I add Reagent, it calls QuantExperimentHome.addReagent(), this functions adds a row to ui with one set of reagent fields and one set of treatment fields, after filling the form fields when I click on "Update Experiment" it calls "quantExperimentHome.update()" and gives the below exception.

            To summarize : We have hierarchy of entities starting from Project > QuantExperiment > Reagent > Treatment
            Project can have multiple QuantExperiments. QuantExperiment can have multiple Reagents and Reagent can have multiple Treatments.
            I have also added all entities for clarity...
            --------------------------------------------------------------
            <ui:repeat value="#{quantExperimentHome.reagent1}" var="info" >
                 <h:inputText id="pname1" required="true" value="#{info.pname}"/>
                 ....other reagent fields

                 <ui:repeat value="#{info.treatment}" var="itreat">
                      <h:inputText id="treatmentCompoundName" value="#{itreat.treatmentCompoundName}"/>
                      ....other treatment fields
                 </ui:repeat>
                 <h:commandLink action="#{quantExperimentHome.addTreatmentLink}" 
                           value="Add treatments" immediate="true" >
                      <f:param name="re_hashcode" value="#{info.hashCode()}"/>                                                                                       
                  </h:commandLink>
            </ui:repeat>
            <h:commandButton styleClass="message" id="addFromQuantExp"
                 action="#{quantExperimentHome.addReagent}"
                 value="+Add More Reagents+">          
            </h:commandButton>

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

            Log on clicking "Update Experiment" it calls "quantExperimentHome.update()"
            --------------------------------------------------------------------------
            [STDOUT] QuantExperimentHome.findMatchingReagent(), boolean = true
            [STDOUT] 1  has not been removed
            [STDOUT] QuantExperimentHome.update(), after clear Reagent.Size=0
            [STDOUT] QuantExperimentHome.update(), New PNAME =PName 1
            [STDOUT] QuantExperimentHome.update(), New TreatmentCompoundName == Comp 1a
            [STDOUT] QuantExperimentHome.update(), New PNAME =PName 2
            [STDOUT] QuantExperimentHome.update(), New TreatmentCompoundName == Comp 2a
            [STDOUT] QuantExperimentHome.update(), after add Reagent.Size=2
            [STDOUT] QuantExperimentHome.update(), in else
            [STDOUT] QuantExperimentHome.update(), Calling super.update
            [STDOUT] Hibernate:
                select
                    MET_REAGENT_SEQ.nextval
                from
                    dual
            12:02:10,605 ERROR [application] javax.persistence.PersistenceException: org.hibernate.PropertyValueException: not-null property references a null or transient value: com.entity.Reagent.quantExperiment
            javax.faces.el.EvaluationException: javax.persistence.PersistenceException: org.hibernate.PropertyValueException: not-null property references a null or transient value: com.entity.Reagent.quantExperiment
                 at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102)
                 at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
            Caused by: javax.persistence.PersistenceException: org.hibernate.PropertyValueException: not-null property references a null or transient value: com.entity.Reagent.quantExperiment
                 at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:629)
                 at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:299)
                 at org.jboss.seam.persistence.EntityManagerProxy.flush(EntityManagerProxy.java:90)
                 at org.jboss.seam.framework.EntityHome.update(EntityHome.java:64)
                 at com.session.QuantExperimentHome.update(QuantExperimentHome.java:372)
            ---------------------------------------------------------------------------------------

            @Name("quantExperimentHome")
            public class QuantExperimentHome extends EntityHome<QuantExperiment> {

                 @In(create = true)
                 ProjectHome projectHome;
                 
                 @Out(required=false)
                 @In(required=false)
                 TreatmentHome treatmentHome;
            ..............

                 public List<Reagent> getReagent() {
                      return getInstance() == null ? null : new ArrayList<Reagent>(
                                getInstance().getReagent());
                 }
                 
                 //CUSTOM FUNCTIONALITY TO ADD MORE EXPERIMENTS
                 @Out(required=false)
                 @In(required=false)     
                ReagentHome reagentHome=new ReagentHome();

                 @In
                 private EntityManager entityManager;

                private Reagent[] reagent1=new Reagent[]{new Reagent()};
               
                 private Reagent initializeReagent(Reagent re){
                      List<Treatment> t=re.getTreatment();               
                      if(t.size()<1){
                           t.add(new Treatment());               
                      }
                      re.setTreatment(t);
                      return re;
                 }
               
                public QuantExperimentHome(){
                      super();
                      Identity.setSecurityEnabled(true);
                      for(int i=0;i<reagent1.length;i++){
                           reagent1[i]=initializeReagent(reagent1[i]);               
                      }
                 }
               
                @Begin(join=true, flushMode=org.jboss.seam.annotations.FlushModeType.MANUAL)
                public String addReagent(){          
                     
                      QuantExperiment qeAdd = getInstance();          
                      
                      Reagent[] reagentNew=null;          
                      
                      if(reagent==null){
                           reagent=new Reagent[]{new Reagent()};
                           return "";
                      }else{
                           reagentNew=new Reagent[reagent.length+1];
                      }

                      for(int i=0;i<reagent1.length;i++){
                           reagentNew[i]=reagent1[i];
                      }
                      Reagent re=new Reagent();
                      re=this.initializeReagent(re);     
                      
                      reagentNew[reagentNew.length-1]=re;          
                      this.reagent1=reagentNew;          
                      
                      return "";
                 }

                 @Override
                @Begin(join=true, flushMode=org.jboss.seam.annotations.FlushModeType.COMMIT)
                public String update(){          
                      
                      QuantExperiment qeUpdate = getInstance();

                      List<Reagent> re=getInstance().getReagent();
                      System.out.println("QuantExperimentHome.update(), Reagent.Size="+re.size());

                      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);
                                //rh.remove();
                           }else{                    
                                System.out.println(reg.getHjid()+"  has not been removed");
                           }
                      }
                      
                      
                      re.clear();
                      System.out.println("QuantExperimentHome.update(), after clear Reagent.Size="+re.size());    
                      
                      for(int i=0;i<this.reagent1.length;i++){
                           reagent1[i].setLabel(qeUpdate.getQuantitationType());
                           reagent1[i].setEntryPoint(reagent1[i].getAffinityCompound());
                           
                           ////////////For testing////////////////////
                           System.out.println("QuantExperimentHome.update(), New PNAME ="+reagent1[i].getPname());               
                           int sz=reagent1[i].getTreatment().size();
                           for(int jj=0;jj<sz;jj++){
                                System.out.println("QuantExperimentHome.update(), New TreatmentCompoundName == "+reagent1[i].getTreatment().get(jj).getTreatmentCompoundName());
                            }               
                           //////////////////////////////////////////               
                           re.add(reagent1[i]);
                           
                      }
                      System.out.println("QuantExperimentHome.update(), after add Reagent.Size="+re.size());

                      System.out.println("QuantExperimentHome.update(), Calling super.update");               
                      return super.update();
                }
                 
                 private boolean findMatchingReagent(Reagent re){
                      System.out.println("QuantExperimentHome.findMatchingReagent(), re.size() = "+re);
                      boolean bl=false;
                      for(int j=0;j<this.reagent1.length;j++){
                           if(reagent1[j].getHjid()==re.getHjid()){
                                bl=true;
                                break;
                           }
                      }
                      System.out.println("QuantExperimentHome.findMatchingReagent(), boolean = "+bl);
                      return bl;
                 }

                public String persist(){
                     
                      java.util.ArrayList<QuantExperiment> arrListQuantExp=new java.util.ArrayList<QuantExperiment>();
                      QuantExperiment qe=getInstance();
                      
                      ArrayList<Reagent> arrListReagent=new ArrayList<Reagent>();
                      
                      for(int i=0;i<reagent1.length;i++){          
                           arrListReagent.add(reagent1[i]);     
                      }
                      
                      super.persist();
                      return "";
                 }
                 public Reagent[] getReagent1() {
                      return reagent1;
                 }

                 public void setReagent1(Reagent[] reagent1) {
                      this.reagent1 = reagent1;
                 }

                 @Begin(join=true, flushMode=org.jboss.seam.annotations.FlushModeType.MANUAL)
                 public String addTreatmentLink(){
                      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("re_hashcode");
                      
                      for(int i=0;i<reagent1.length;i++){
                              if(reagent1[i].hashCode()==Integer.parseInt(reagentHashcode)){
                                    Treatment treatment = new Treatment();
                                    treatment.setReagent(reagent1[i]);
                                    reagent1[i].getTreatment().add(treatment);
                                    break;
                              }
                      }
                      return "";     
                 }
            }
            ------------------------------------------------

            ENTITIES
            ========

            Project Entity
            --------------
            protected List<QuantExperiment> quantExperiment;
            .....
            @OneToMany(cascade = {CascadeType.ALL}, mappedBy="project")
            public List<QuantExperiment> getQuantExperiment() {
                    if (quantExperiment == null) {
                        quantExperiment = new ArrayList<QuantExperiment>();
                    }
                     return this.quantExperiment;
                }
            public void setQuantExperiment(List<QuantExperiment> quantExperiment) {
                     
                     for(QuantExperiment q : quantExperiment){
                          q.setProject(this);
                     }
                    this.quantExperiment = quantExperiment;
                }

            QuantExperiment Entity
            ----------------------
            protected List<Reagent> reagent;
            protected Project project;

            @ManyToOne(optional=false)
            public Project getProject() {
                      return project;
                 }
            public void setProject(Project project) {
                      this.project = project;
                 }

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

            Reagent Entity
            ----------------
            protected List<Treatment> treatment;
            @OneToMany(cascade = {CascadeType.ALL}, mappedBy="reagent")
            public List<Treatment> getTreatment() {
                    if (treatment == null) {
                        treatment = new ArrayList<Treatment>();
                    }
                    return this.treatment;
                }
            public void setTreatment(List<Treatment> treatment) {
                      for(Treatment t : treatment){
                         t.setReagent(this);
                      }
                       this.treatment = treatment}

            Treatment entity
            -------------------
            public class Treatment impl...{
            protected Reagent reagent;
            @ManyToOne(cascade = {CascadeType.PERSIST},optional=false)
               public Reagent getReagent() {
                  return reagent;
               }
            public void setReagent(Reagent reagent) {
                  this.reagent = reagent;
               }