title: Spring Boot Test + MyBatis: mapper doesn't do repeated SELECT when @Transactional
body: <p>I'm currently writing tests for a Spring Boot project which uses MyBatis as the ORM.</p>
  
  <p>I'm testing the controller layer with MockMvc.</p>
  
  <p>Now, when I do a transaction I naturally add @Transactional to the test so everything gets rolled back after the test. However, the Service method which validates the update also has the Spring @Transactional annotation.</p>
  
  <p>The testing method first gets an object from the object mapper and changes it.
  Next, I use MockMvc to call the corresponding controller update path and add a json of the changed object as the content.</p>
  
  <p>The controller tells the service to do the update. First thing the service does is get the current object with that id from the mapper. Here is where the problem happens: instead of doing a SELECT to get the object it returns the same reference as it did in the test. Hence, I have the same object twice. This leads to a bunch of redundancy which will still get most of the fields updated, except for dependencies that check for changes and update another table.
  So in turn I need the mapper to get the current object from the database with the given ID, NOT a cached version of a previous call during the same @Transactional period.</p>
  
  <p>Is there a way to either force this to happen (one SELECT per "getByPrimaryKey", etc., instead of caching), or to have the @Transactionals be independent?
  When removing the @Transactional from the test it works just fine as it does another db request.</p>
  
  <p><strong>edit</strong>:</p>
  
  <p>MyBatis does cache during Transactions, as it seems, on default. However, this shouldn't necessarily be disabled just so a test works. As the cache is flushed when performing an insert, update or delete it's possible to do an update after the first getById. However, this doesn't seem like the best solution, imo.</p>
  
  <p>Is there an option to disable the caching for a certain profile?</p>