title: JPA updates a column/attribute that is not modified
body: <p>We have a class <code>User</code>, which has a field <code>isMigrated</code> which is <code>0</code> by default. Now in the service layer, we are trying to update the user's <code>status</code> (another attribute). So these are the steps</p>
  <ol>
  <li>We fetch the user by his ID (Primary key).</li>
  <li>We set the new status.</li>
  <li>We call the <code>save</code> method in the respective repository for that particular user.</li>
  </ol>
  <p>Now the problem is that the column that corresponds to <code>isMigrated</code>, which is currently having a value of <code>1</code> is also getting updated to <code>0</code>. We have clearly not updated that attribute and so we came to a conclusion that it is the default value that is going to the DB during updation, but how is that possible?</p>
  <p>We tried to check some other information about <a href="https://stackoverflow.com/questions/27818334/jpa-update-only-specific-fields">JPA: update only specific fields</a> but wasn't helpful in this case. We had to go with native query to fix this problem.</p>
  <p><strong>Entity class</strong></p>
  <pre><code>@Entity
  @Table(name = &quot;USER_TABLE&quot;)
  public class User implements Serializable {
  
      //  other attributes
  
      @Column(name = &quot;USER_STATUS&quot;)
      private int status;
  
      //  [1 = migrated, 0 = new (default)]
      @Column(name = &quot;IS_MIGRATED&quot;)
      private int isMigrated = 0;
  
      //  getters and setters
  }
  </code></pre>
  <p><strong>Repository</strong></p>
  <pre><code>@Repository
  public interface UserRepository
          extends PagingAndSortingRepository&lt;User, String&gt;, JpaSpecificationExecutor&lt;User&gt; {
  }
  </code></pre>
  <p><strong>Updates regarding how the user is being fetched</strong></p>
  <p>The user is fetched by his ID from the request, we'll run a native query by joining two other tables by fetching only the required fields. So in the process of fetch, we are not using the <code>isMigrated</code> column, plus that attribute (call hierarchy) is set only when a user is created and this is a different API.</p>
  <p>Once we fetch the user, it's reference is set to an attribute of another bean <code>RequestInfo</code> with a Request scope (<code>@Scope(value = WebApplicationContext.SCOPE_REQUEST, proxyMode = ScopedProxyMode.TARGET_CLASS)</code>), which will be used all along the API. Now in the service layer we use the <code>RequestInfo</code> to get the user, then we set the new status and call the <code>save</code> method.</p>
  <pre><code>@Query(value = &quot;select um.USER_ID,FIRST_NAME,LAST_NAME,USER_STATUS,um.MOB,EMAIL_ID,ENTITY_TYPE,PROFILE_ID,PARENT_ID,SOFT_DELETE, NATIONALITY_ID,DOB,GENDER,FATHER_NAME,QUALIFICATION,LANGUAGE_ID,DESIGNATION,CONTRACT_START_DATE, &quot;
          + &quot;CONTRACT_END_DATE, REGISTERED_DATE,ACTIVATED_DATE,CREATED_BY,CONTACT_EMAIL_ID,CONTACT_MSISDN,ERP_CODE, &quot;
          + &quot;LAST_FINANCIAL_TXN_DATE,MM_APP_VERSION,MM_APP_LAST_UPDATE_DATE,OFFICIAL_EMAIL_ID,SRC_OF_FUND,POW, JOB_NAME,JOB_STARTED,MONTHLY_INCOME, &quot;
          + &quot;MARITAL_STATUS,SALUTE,HIER_ID,LABEL_ID,PROFILE_PICTURE_ID,DOC_NAME,lm.ID , lm.ALERT_DATE , lm.BLOCKED_DATE , lm.IS_FORCE_PIN , lm.IS_FORCE_PASSWORD , lm.LAST_LOGIN_TIME , lm.LAST_NOTIFIED_DATE ,lm.PASSWORD , lm.PIN ,  &quot;
          + &quot;lm.WRONG_PIN_ATTEMPT , lm.PIN_EXPIRY_DATE , lm.WRONG_PWD_ATTEMPT , lm.PWD_EXPIRY_DATE , lm.USER_NAME, am.ID aid, am.ADDRESS_1, am.ADDRESS_2, am.ADDRESS_TYPE, am.CITY , am.PIN_CODE , am.POST_BOX , am.STATE , am.STREET_NAME,ui.ID_TYPE, ui.ID_NO,um.NOTIFICATION_SERVICE_TYPE &quot;
          + &quot; FROM USER_TABLE um inner join USER_LOGIN_TABLE lm ON um.USER_ID=lm.USER_ID and um.USER_ID=?1 &quot;
          + &quot; left outer join USER_ADDRESS_TABLE am ON um.USER_ID=am.USER_ID left outer join USER_IDENTI_CATION_DETAILS ui ON um.USER_ID = ui.USER_ID&quot;, nativeQuery = true)
  List&lt;Object[]&gt; getUserByUserId(String userId);
  </code></pre>
  <p><strong>Updates regarding the columns that are not fetched while getting a user - Asked by Ratul Sharker</strong></p>
  <p>There are a few columns that we are not fetching, like <code>deviceId</code>, <code>deviceIp</code>, etc. These are <code>String</code> types, so even though we are not providing a default value to these fields <code>null</code> will be assigned as the default value. So my doubt is why the default value is inserted into <code>isMigrated</code> and why the same is not inserted to the other two fields because these three are not fetched in the above query right?</p>