title: How to Stop Hibernate From Trying to Update one-to-many Over Join Table
body: <p>Ok so I'm having bit of a problem with my Hibernate mappings and getting the desired behavior.</p>
  
  <p>Basically what I have is the following Hibernate mapping:</p>
  
  <pre><code>&lt;hibernate-mapping&gt;
      &lt;class name="com.package.Person" table="PERSON" schema="MYSCHEMA" lazy="false"&gt;
          &lt;id name="personId" column="PERSON_ID" type="java.lang.Long"&gt;
              &lt;generator class="sequence"&gt;
                  &lt;param name="sequence"&gt;PERSON_ID_SEQ&lt;/param&gt;
              &lt;/generator&gt;
          &lt;/id&gt;
  
          &lt;property name="firstName" type="string" column="FIRST_NAME"&gt;
          &lt;property name="lastName" type="string" column="LAST_NAME"&gt;
          &lt;property name="age" type="int" column="AGE"&gt;
  
          &lt;set name="skills" table="PERSON_SKILL" cascade="all-delete-orphan"&gt;
              &lt;key&gt;
                  &lt;column name="PERSON_ID" precision="12" scale="0" not-null="true"/&gt;
              &lt;/key&gt;
              &lt;many-to-many column="SKILL_ID" unique="true" class="com.package.Skill"/&gt;
          &lt;/set&gt;
      &lt;/class&gt;
  &lt;/hibernate-mapping&gt;
  
  
  &lt;hibernate-mapping&gt;
      &lt;class name="com.package.Skill" table="SKILL" schema="MYSCHEMA"&gt;
          &lt;id name="skillId" column="SKILL_ID" type="java.lang.Long"&gt;
              &lt;generator class="sequence"&gt;
                  &lt;param name="sequence"&gt;SKILL_ID_SEQ&lt;/param&gt;
              &lt;/generator&gt;
          &lt;/id&gt;
          &lt;property name="description" type="string" column="DESCRIPTION"&gt;
      &lt;/class&gt;
  &lt;/hibernate-mapping&gt;
  </code></pre>
  
  <p>So lets assume that I have already populated the Skill table with some skills in it. Now when I create a new Person I want to associate them with a set of skills that already exist in the skill table by just setting the ID of the skill. For example:</p>
  
  <pre><code>Person p = new Person();
  p.setFirstName("John");
  p.setLastName("Doe");
  p.setAge(55);
  
  //Skill with id=2 is already in the skill table
  Skill s = new Skill()
  s.setSkillId(2L);
  
  p.setSkills(new HashSet&lt;Skill&gt;(Arrays.asList(s)));
  PersonDao.saveOrUpdate(p);
  </code></pre>
  
  <p>If I try to do that however I get an error saying:</p>
  
  <pre><code>WARN (org.slf4j.impl.JCLLoggerAdapter:357) - SQL Error: 1407, SQLState: 72000
  ERROR (org.slf4j.impl.JCLLoggerAdapter:454) - ORA-01407: cannot update ("MYSCHEMA"."SKILL"."DESCRIPTION") to NULL
  
  ERROR (org.slf4j.impl.JCLLoggerAdapter:532) - Could not synchronize database state with session
  org.hibernate.exception.GenericJDBCException: Could not execute JDBC batch update
  </code></pre>
  
  <p>The reason I am getting this error I think is because Hibernate sees that the Skill with Id 2 has 'updated' its description to null (since I never set it) and tries to update it. But I don't want Hibernate to update this. What I want it to do is insert the new Person p and insert a record into the join table, PERSON_SKILL, that matches p with the skill in the SKILL table with id=2 without touching the SKILL table.</p>
  
  <p>Is there anyway to achieve this behavior?</p>