title: EntityManager from persistence Context not working with Spring Async Method
body: <p>In my regular SpringBoot application , I need to capture the user activity asynchronously. My DB tables are related badly that it is not possible to leverage SPring data JPA. Hence I am using EntityManager from persistence context to execute the DB operation. However the inserts in the child table is not being done.
  The below code is being invoked from a method annotated with @Async
  Here is the code :
  @PersistenceContext
  private EntityManager entityManager;</p>
  <pre><code>private Query query;
  
  @Override
  @Transactional
  public void saveUserClicks(Pojo pojo)
          throws DataIntegrityViolationException {
  
      
      StringBuilder q1 = new StringBuilder(
              &quot;query1&quot;);
      StringBuilder q2 = new StringBuilder(
              &quot;query2&quot;);
  
      Query q1NativeQuery = getNativeQuery(entityManager.createNativeQuery(q1.toString()));
      q1NativeQuery.setParameter(1, param1);
      q1NativeQuery.setParameter(2, param2);
      
      int executed = q1NativeQuery.executeUpdate();
      System.out.println(&quot;Value of executed:&quot; + executed);
  
      Set&lt;ChildPOJO&gt; set = obj1.getSet();
      if (null != set) {
          for (ChildPOJO child : set) {
                  q1NativeQuery = getNativeQuery(entityManager.createNamedQuery(q2.toString()));
                  q1NativeQuery.setParameter(1, param1);
                  q1NativeQuery.setParameter(2, param2);
                  executed = q1NativeQuery.executeUpdate();
                  System.out.println(&quot;Value of executed :&quot; + executed);
              }
          }
      }
  }
  </code></pre>