title: Hibernate list mapping constraint violation
body: <p>These are my postresql tables :</p>
  
  <pre><code>CREATE TABLE "user" (
      "id" int4 NOT NULL,
      "activated" bool NOT NULL DEFAULT True,
      CONSTRAINT "user_pkey" PRIMARY KEY("id")
  );
  
  CREATE TABLE "user_data" (
      "id" int4 NOT NULL,
      "user_id" int4 NOT NULL,
      "idx" int4 DEFAULT NULL,
      "first_name" text NOT NULL,
      "last_name" text NOT NULL,
      CONSTRAINT "user_data_pkey" PRIMARY KEY("id")
  );
  </code></pre>
  
  <p>My goal is to have in my "User" hibernate bean a direct reference to user's data, like this <code>List&lt;UserData&gt;</code>. Here is my hibernate mapping :</p>
  
  <pre><code>&lt;hibernate-mapping package="mypackage"&gt;
      &lt;class name="User" table="user"&gt;
          &lt;id name="id" type="long" column="id"&gt;
              &lt;generator class="sequence"&gt;
                  &lt;param name="sequence"&gt;user_id_seq&lt;/param&gt;
              &lt;/generator&gt;
          &lt;/id&gt;
  
          &lt;property name="activated" column="activated" type="boolean" /&gt;
  
          &lt;list name="data" table="user_data" cascade="all"&gt;
              &lt;key column="user_id" /&gt;
              &lt;list-index column="idx" /&gt;
              &lt;one-to-many class="UserData" /&gt;
          &lt;/list&gt;
      &lt;/class&gt;
  
      &lt;class name="UserData" table="user_data"&gt;
          &lt;id 
              name="id" 
              type="long"
              column="id"&gt;
              &lt;generator class="sequence"&gt;
                  &lt;param name="sequence"&gt;user_data&lt;/param&gt;
              &lt;/generator&gt;
          &lt;/id&gt; 
          &lt;many-to-one name="user" column="user_id" class="User" /&gt;
          &lt;property name="firstName" column="first_name" type="text" /&gt;
          &lt;property name="lastName" column="last_name" type="text" /&gt;
      &lt;/class&gt;    
  &lt;/hibernate-mapping&gt;
  </code></pre>
  
  <p>I ran some tests. When I add a new "UserData" to my list and then execute <code>session.saveOrUpdate(myUser)</code> it works, but when I remove an entry from my list and execute the same line, it throws <code>ERROR [JDBCExceptionReporter] ERROR: null value in column "user_id" violates not-null constraint</code> just after telling me that <code>batch update user_data set user_id=null, idx=null where user_id='20' and id='46' was canceled</code></p>
  
  <p>What is the right usage of this type of mapping? Thank you</p>
  
  <p><strong>EDIT:</strong> Adding "inverse=true" isn't adapted to my usage because I need my update on user trigger an update on UserData. Again, maybe I didn't understand the right usage of this type of usage.</p>