-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 11 posts ] 
Author Message
 Post subject: please help org.hibernate.PropertyValueException: not-null..
PostPosted: Tue Nov 25, 2008 9:34 pm 
Newbie

Joined: Tue Nov 25, 2008 9:31 pm
Posts: 18
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;
}


Top
 Profile  
 
 Post subject: Re: please help org.hibernate.PropertyValueException: not-nu
PostPosted: Wed Nov 26, 2008 12:11 am 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
I assume your problem is here. You are linking only one side of the relation:

Code:
reagent1[i].getTreatment().add(new Treatment());


may be this will solve the problem;


Code:
Treatment treatment = new Treatment();
treatment.setReagent(reagent1[i]);
reagent1[i].getTreatment().add(treatment);




Bests,
Farzad-


Top
 Profile  
 
 Post subject: please help org.hibernate.PropertyValueException: not-null..
PostPosted: Wed Nov 26, 2008 12:33 am 
Newbie

Joined: Tue Nov 25, 2008 9:31 pm
Posts: 18
You are great sir....

I am close to it, I am not getting any error, row is added oly issue..

a) It adds the row but values of previous rows are populated by default.
b) It calls hibernate and enters record in treatment table... it should only add row to ui... and should not do any insert...

Plssss..... help

Update QuantExperimentHome.addTreatmentLink().
---------------------------------------------
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 "";
}

Log:
---
23:25:09,230 INFO [STDOUT] QuantExperimentHome.addTreatmentLink(), reagent[1]-834261464, HASCODE FROM FORM=-834261464
23:25:09,230 INFO [STDOUT] QuantExperimentHome.addTreatmentLink() 1, reagent[1].treatment.lenth2
23:25:09,230 INFO [STDOUT] QuantExperimentHome.addTreatmentLink() 2, reagent[1].treatment.lenth3
23:25:09,230 INFO [STDOUT] Hibernate:
select
MET_TREATMENT_SEQ.nextval
from
dual
23:25:09,262 INFO [STDOUT] Hibernate:
insert
into
MET_TREATMENT
(NOTE, LASTUPDATEDTIMESTAMP, reagent_HJID, TREATMENTCONCENTRATION, TREATMENTCONCENTRATIONUNIT, TREATMENTCOMPOUNDNAME, TREATMENTTIMEUNIT, TREATMENTDURATION, HJID)
values
(?, ?, ?, ?, ?, ?, ?, ?, ?)


Top
 Profile  
 
 Post subject: Re: please help org.hibernate.PropertyValueException: not-nu
PostPosted: Wed Nov 26, 2008 11:02 am 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
valatharv wrote:
a) It adds the row but values of previous rows are populated by default.
b) It calls hibernate and enters record in treatment table... it should only add row to ui... and should not do any insert...
]


I don't understand your question in a but as for the second one I guess reagent1[i] is a managed object and that's why you have all this problem in the first place. You could do two things here, clear you session object or set it to read only by setting autoFlush to Manual. However, you need to flush the changes when it is required.



Farzad-


Top
 Profile  
 
 Post subject: Re: please help org.hibernate.PropertyValueException: not-nu
PostPosted: Wed Nov 26, 2008 12:16 pm 
Newbie

Joined: Tue Nov 25, 2008 9:31 pm
Posts: 18
Here is more detail...
Sorry if I was not clear.. pls help in trouble... :(

a) "It adds the row but values of previous rows are populated by default."
Reagent entity contains treatment and 1 reagent can have multiple treatments

If h:inputText id="treatmentCompoundName" contains say "aaa" and when I click "Add treatments" link, it adds new h:inputText for treatment compound as per ui:repeat
but new h:inputText for treatment compound displays the same value as "aaa" it should be null.

Code:
<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>


b) "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..

"I guess reagent1[i] is a managed object and that's why you have all this problem in the first place. You could do two things here, clear you session object or set it to read only by setting autoFlush to Manual. However, you need to flush the changes when it is required."

I think you are right reagent1[i] should be a managed object as it is update screen... as I am new to this, please guide me for the 2 options on how and which place I should add the code..

Code:
@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++){
if(reagent1[i].hashCode()==Integer.parseInt(reagentHashcode)){
   /////////////
   Treatment treatment = new Treatment();
   treatment.setReagent(reagent1[i]);
   reagent1[i].getTreatment().add(treatment);
   ///////////
   //reagent1[i].getTreatment().add(new Treatment());
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
----------------
Code:
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
-------------------
Code:
public class Treatment impl...{
protected Reagent reagent;
@ManyToOne(optional=false)
   public Reagent getReagent() {
      return reagent;
   }
public void setReagent(Reagent reagent) {
      this.reagent = reagent;
   }


Last edited by valatharv on Wed Nov 26, 2008 5:13 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: please help org.hibernate.PropertyValueException: not-nu
PostPosted: Wed Nov 26, 2008 12:19 pm 
Newbie

Joined: Tue Nov 25, 2008 9:31 pm
Posts: 18
duplicate of above


Last edited by valatharv on Wed Nov 26, 2008 4:11 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: please help org.hibernate.PropertyValueException: not-nu
PostPosted: Wed Nov 26, 2008 12:25 pm 
Newbie

Joined: Tue Nov 25, 2008 9:31 pm
Posts: 18
duplicate of above


Last edited by valatharv on Wed Nov 26, 2008 4:07 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject: please help org.hibernate.PropertyValueException: not-null..
PostPosted: Wed Nov 26, 2008 12:36 pm 
Newbie

Joined: Tue Nov 25, 2008 9:31 pm
Posts: 18
I am sorry, don't know how post was repeated I got the firewall error so I just tried posting again...


Top
 Profile  
 
 Post subject: please help org.hibernate.PropertyValueException: not-null..
PostPosted: Wed Nov 26, 2008 12:36 pm 
Newbie

Joined: Tue Nov 25, 2008 9:31 pm
Posts: 18
I am sorry, don't know how post was repeated I got the firewall error so I just tried posting again...


Top
 Profile  
 
 Post subject: Re: please help org.hibernate.PropertyValueException: not-nu
PostPosted: Wed Nov 26, 2008 4:53 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
valatharv wrote:
I am sorry, don't know how post was repeated I got the firewall error so I just tried posting again...



can you edit you original post and put the code snippets in code blocks so that they come up nicely. I have problems reading code as if I were coding in a terminal world.





Farzad-


Top
 Profile  
 
 Post subject: Re:please help org.hibernate.PropertyValueException:not-null
PostPosted: Wed Nov 26, 2008 5:18 pm 
Newbie

Joined: Tue Nov 25, 2008 9:31 pm
Posts: 18
Done Farzad, please check the code above... help...

Please help... I think as you said reagent is managed that is why it calls hibernate and inserts record on clicking "Add treatment" link.. :(

thanks in advance


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 11 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.