title: Spring boot JPA saved entities not getting when retrieve with EntityManager native query in a @Async method
body: <p>I have a service class like this.</p>
  <pre><code>@Service
  public class ServiceAImpl implements ServiceA {
  
      @Autowired
      private ServiceARepository repo;
  
      @Autowired
      private Publisher publisher;
  
      @Transactional
      public String save(Model model) {
          //entity list creation code goes here
      
          List&lt;EntityA&gt; entityList = repo.save(entityList);
          repo.flush();
      
          ...
          //savedIdList is comma separated list of entityList EntityA.id
      
          publisher.publish(savedIdList);
      
          return responseObject.toString();
      }
  
  
  }
  </code></pre>
  <p>When controller call to this service its create the Entity list and save. After that its call to publish method in another class with the saved ids. This save method annotated with @Transactional.</p>
  <pre><code>@Service
  public class Publisher {
  
      @Autowired
      private AsyncPublisher asyPublisher;
      
      @Autowired
      PublishedDataRepository repo;
      
  
      public void publish(String savedIdList) throws Exception {
          savePublishData(..., savedIdList);
          
      }
  
      @Transactional
      private void savePublishData(..., String savedIdList) throws Exception {
          
          SearchPublishedData publishData = new SearchPublishedData();
          ...
          publishData.setIdList(savedIdList);
          
          publishData = repo.saveAndFlush(publishData);
          
          asyPublisher.callPublisher(..., savedIdList, publishData.getId());
          
      }
      
  }
  </code></pre>
  <p>In publisher class its save a record to the publisher table and again it call to the async publisher class. In this class there is a method with @Async and its implemented with ThreadPoolTaskExecutor. In this async method what it going to do is get the previously saved data from its ids using EntityManager native query.</p>
  <p>This is a sample java class code. Actually in this native query there are few tables join with this previously saved table and getting the data.</p>
  <pre><code>@Service
  public class AsyncPublisher {
  
      @PersistenceContext
      private EntityManager entityManager;
  
  
      @Async(&quot;threadPoolTaskExecutor&quot;)    //This is created as ThreadPoolTaskExecutor
      public void callPublisher(..., String ids, Long savedId) {
          
          try {
              String query = getQuery(ids);
              
              List&lt;Object[]&gt; results = entityManager.createNativeQuery(query).getResultList();
  
              ... ///Other codes goes here, but results is empty
              
          } catch (Exception e) {
              logg error
          }
      }
      
      private String getQuery(String ids) throws Exception {
          StringBuilder query = new StringBuilder();
  
          query.append(&quot;SELECT * FROM EntityA_table WHERE id IN (&quot;).append(ids).append(&quot; ) &quot;);
          //This is a sample query, actually in this query there are 2 or more tables joined
          return query.toString();
      }
  }
  </code></pre>
  <p>My problem is when I retrieve data from EntityManager native query time to time its not getting the data. But when I check the database with those ids those data available in database.</p>
  <p>Anyone can give me the reason for this.</p>
  <p>I think this saving block is annotated with @Transactional and it going to commit data to the Database at the end of the method execution and but before it save to the database EntityManager native query execution happens in another thread and its execute before the data commit. Is this correct? If not please explain someone what is happening here.</p>
  <p>Also is there any way to avoid this data not retrieve scenario?</p>
  <p>Thank you.</p>