title: Spring JpaRepository - Detach and Attach entity
body: <p>I am using spring boot and hibernate over jpa. I am using JpaRepository interface to implement my repositories. As with following UserRepository</p>
  
  <pre><code>public interface UserRepository extends JpaRepository&lt;User, Long&gt; {
  }
  </code></pre>
  
  <p>I want to achieve following</p>
  
  <ol>
  <li>Load a User entity.</li>
  <li>Change the state of entity object e.g. user.setName("foo")</li>
  <li>Do an external system webservice call. Save the call result in DB</li>
  <li>Only on successful response of this webservice call, save the new state of user in repository.</li>
  </ol>
  
  <p>All above steps are not happening in one transaction i.e. the external service call is out of transaction. </p>
  
  <p>When I save my webservice result in DB via its repository, my changes in User entity are also saved. As per my understanding this is due to the flushing of underlaying persistence context at step # 3. After some google, I think I can achieve my purpose, if I can detach my user entity at step one and reattach it at step 4. 
  Please confirm if my understanding is correct and how I can achieve this? There is not method in JpaRepository interface to detach an entity.</p>
  
  <p>Following is the code to illustrate</p>
  
  <pre><code>public void updateUser(int id, String name, int changeReqId){
      User mUser = userRepository.findOne(id); //1
      mUser.setName(name); //2
  
      ChangeRequest cr = changeRequestRepository.findOne(changeReqId);
      ChangeResponse rs = userWebService.updateDetails(mUser); //3
  
      if(rs.isAccepted()){
          userRepository.saveAndFlush(mUser); //4
      }
  
      cr.setResponseCode(rs.getCode());
      changeRequestRepository.saveAndFlush(cr); //this call also saves the changes at step 2
  }
  </code></pre>
  
  <p>Thanks</p>