title: Spring Data JPA updates entity when I don't want it to
body: <p>I have a situation where I want to retrieve data, but before it is returned I want to change it without saving/persisting.</p>
  
  <p>Here is my rest controller:</p>
  
  <pre><code>@RestController
  @RequestMapping("api/drawing-releases")
  @CrossOrigin("*")
  public class DrawingReleaseRestController {
      @Autowired
      private DrawingReleaseService service;
  
      @RequestMapping(method = RequestMethod.GET, value = "/{id}")
      @JsonView(View.DrawingReleaseView.class)
      public ResponseEntity&lt;DrawingRelease&gt; getWithId(@PathVariable int id) throws EntityNotFoundException {
        return new ResponseEntity&lt;&gt;(service.getByID(id),HttpStatus.OK);
      }
  }
  </code></pre>
  
  <p>Here is the service implementation:</p>
  
  <pre><code>@Service
  public class DrawingReleaseServiceImpl extends DrawingFunctions implements DrawingReleaseService {
      /**
       * Logging Manager
       */
      private static final Logger LOGGER=LogManager.getLogger();
  
      @Autowired
      private DrawingReleaseRepository repository;
  
      @Override
      public DrawingRelease getByID(int id) throws EntityNotFoundException {
          Optional&lt;DrawingRelease&gt; opt = repository.findById(id);
          ...
          // manipulate the data here
          ...
          return opt.get();
      }
  }
  </code></pre>
  
  <p>Intitially I had the <code>@Transactional</code> annotation on the service.  By removing that and not having it on the <code>getByID</code> method, I was first thinking that the session would open and close with the repository as indicated by the answer <a href="https://stackoverflow.com/questions/25709976/spring-boot-spring-data-how-are-hibernate-sessions-managed">here</a>.</p>
  
  <p>That did not work, and I saw in the comments that "the session lasts for the entire duration of HTTP request processing." So, in the service I added</p>
  
  <pre><code>@Autowired
  private EntityManager em;
  </code></pre>
  
  <p>and then in the <code>getByID</code> method I added</p>
  
  <pre><code>em.close();
  </code></pre>
  
  <p>before I made any changes.  However, any changes I make are still being persisted.</p>
  
  <p>Is there any way that I can make unsaved changes in my service layer?  I suppose I could create some POJOs that mirror the entities (but aren't entities) and then copy the data and return those objects, but it doesn't seem like I should have to do something like that.</p>