-->
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.  [ 2 posts ] 
Author Message
 Post subject: null value in column "" violates not-null constraint
PostPosted: Fri Jul 16, 2010 12:17 pm 
Newbie

Joined: Fri Jul 16, 2010 11:51 am
Posts: 2
This appears to be a common problem, and I understand it is usually with the mapping, but I cannot figure it out in my case. I have googled both this forum and the internet at large, plus read through "Java persistence with Hibernate" by Bauer and King, but I am still stumped.

I have a "typed" one-to-one association as described here. I have the key values set to not-null, but when I go to save to the database I get the following error where you can clearly see that hibernate is trying to enter null values:
Quote:
10:42:59,120 ERROR [JDBCExceptionReporter] Batch entry 0 insert into public.exam_diagnosis_and_management_options (diagnostic_code_id, index, exam_medical_decision_id) values (6, NULL, NULL) was aborted. Call getNextException to see the cause.
10:42:59,120 WARN [JDBCExceptionReporter] SQL Error: 0, SQLState: 23502
10:42:59,120 ERROR [JDBCExceptionReporter] ERROR: null value in column "exam_medical_decision_id" violates not-null constraint


From what I have read, I am guessing that Hibernate is trying to do an insert, and then plans on doing an update to set those, though I cannot figure out a way to make it do it all in one insert. I cannot use inverse=true since it is a one-to-one relationship. I am using session.merge() to save the code. Any help would be greatly appreciated.

My mapping files are:
Code:
<hibernate-mapping>
    <class name="com.wg.common.bean.ExamMedicalDecision" table="exam_medical_decision">
        <id name="id" type="integer">
            <column name="id" />
            <generator class="sequence">
                <param name="sequence">exam_medical_decision_id_seq</param>
            </generator>
        </id>
...
        <one-to-one name="diagnosisAndOptions1" cascade="all" lazy="false">
           <formula>1</formula>
           <formula>id</formula>
        </one-to-one>
        <one-to-one name="diagnosisAndOptions2" cascade="all" lazy="false">
           <formula>2</formula>
           <formula>id</formula>
        </one-to-one>
        <one-to-one name="diagnosisAndOptions3" cascade="all" lazy="false">
           <formula>3</formula>
           <formula>id</formula>
        </one-to-one>
        <one-to-one name="diagnosisAndOptions4" cascade="all" lazy="false">
           <formula>4</formula>
           <formula>id</formula>
        </one-to-one>
    </class>
</hibernate-mapping>

<hibernate-mapping>
    <class name="com.wg.common.bean.ExamDiagnosisAndManagementOptions" table="exam_diagnosis_and_management_options" >
        <composite-id >
           <key-property name="index" type="integer">
              <column name="index" not-null="true"></column>
           </key-property>
           <key-many-to-one name="examMedicalDecision" class="com.wg.common.bean.ExamMedicalDecision">
              <column name="exam_medical_decision_id" not-null="true"></column>
             </key-many-to-one>
        </composite-id>
        <many-to-one name="diagnosticCode" class="com.wg.common.bean.DiagnosticCode" lazy="false">
            <column name="diagnostic_code_id"/>
        </many-to-one>
        <set name="options" table="exam_diagnosis_and_management_options_xref" lazy="false">
           <key>
              <column name="exam_diagnosis_and_management_options_index" not-null="true"></column>
              <column name="exam_diagnosis_and_management_options_emd_id" not-null="true"></column>
           </key>
           <element column="exam_management_option" type="text" not-null="true"/>
        </set>
    </class>
</hibernate-mapping>



Top
 Profile  
 
 Post subject: Re: null value in column "" violates not-null constraint
PostPosted: Fri Jul 16, 2010 3:48 pm 
Newbie

Joined: Fri Jul 16, 2010 11:51 am
Posts: 2
Well, I found a way that works, but I am not sure I could call it fixed. My guess is this is a bug in Hibernate.

As outlined here and using the "second approach" where you add an id class that is mapped but leaving the index and examMedicalDecisionId fields in the ExamDiagnosisAndManagementOptions class it mysteriously starts working. From what I read the two approaches should be logically identical, but apparently not.

This is my new mapping file for ExamDiagnosisAndManagementOptions (the only mapping file changed). Note the bolded part. I also created a ExamDiagnosisAndManagementOptionsId class below also. It has no mapping file.

Quote:
<hibernate-mapping>
<class name="com.wg.common.bean.ExamDiagnosisAndManagementOptions" table="exam_diagnosis_and_management_options" >
<composite-id
mapped="true"
class="com.wg.common.bean.ExamDiagnosisAndManagementOptionsId"
>
<key-property name="index" type="integer">
<column name="index" not-null="true"></column>
</key-property>
<key-many-to-one name="examMedicalDecision" class="com.wg.common.bean.ExamMedicalDecision">
<column name="exam_medical_decision_id" not-null="true"></column>
</key-many-to-one>
</composite-id>
<many-to-one name="diagnosticCode" class="com.wg.common.bean.DiagnosticCode" lazy="false">
<column name="diagnostic_code_id"/>
</many-to-one>
<set name="options" table="exam_diagnosis_and_management_options_xref" lazy="false">
<key>
<column name="exam_diagnosis_and_management_options_index" not-null="true"></column>
<column name="exam_diagnosis_and_management_options_emd_id" not-null="true"></column>
</key>
<element column="exam_management_option" type="text" not-null="true"/>
</set>
</class>
</hibernate-mapping>

Code:
public class ExamDiagnosisAndManagementOptionsId implements Serializable {

    private volatile Integer index;
    private volatile ExamMedicalDecision examMedicalDecision;
    public Integer getIndex() {
        return index;
    }
    public void setIndex(Integer index) {
        this.index = index;
    }
    public ExamMedicalDecision getExamMedicalDecision() {
        return examMedicalDecision;
    }
    public void setExamMedicalDecision(ExamMedicalDecision emd) {
        this.examMedicalDecision = emd;
    }
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result
            + ((examMedicalDecision == null) ? 0 : examMedicalDecision.hashCode());
        result = prime * result + ((index == null) ? 0 : index.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (obj == null) return false;
        if (getClass() != obj.getClass()) return false;
        ExamDiagnosisAndManagementOptions other = (ExamDiagnosisAndManagementOptions) obj;
        if (examMedicalDecision == null) {
            if (other.getExamMedicalDecision() != null) return false;
        }
        else if (!examMedicalDecision.equals(other.getExamMedicalDecision())) return false;
        if (index == null) {
            if (other.getIndex() != null) return false;
        }
        else if (!index.equals(other.getIndex())) return false;
        return true;
    }
}



Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.