title: Spring + JPA: object does not get fetched from the database
body: <p>I am implementing an online shop. I have the following code for saving an order:</p>
  
  <pre><code>@Service
  @Transactional
  public class OrderServiceImpl extends GenericServiceImpl&lt;Order, Integer&gt; implements OrderService {
  
      @Inject
      private ItemRepository itemRepository;
  
      @Override
      public void saveOrder(Order order) {
          this.updateItemsAccordingToOrderedQuantities(order);
          repository.save(order);
      }
  
      private void updateItemsAccordingToOrderedQuantities(Order order) {
          List&lt;OrderedItem&gt; orderedItems = order.getOrderedItems();
          for (OrderedItem orderedItem : orderedItems) {
              // fetch from database
              Item item = itemRepository.findOne(orderedItem.getItem().getId()); 
  
              item.reduceWeightInColdStoreBy(orderedItem.getWeight());
              itemRepository.update(item);
          }
      }
  }
  </code></pre>
  
  <p>Before actually saving the order I update the "weight" property of each item (some quantity gets sold with this order, so there is less left). </p>
  
  <p><code>OrderedItem</code> objects hold the reference to <code>Item</code>, but I want to fetch the fresh <code>Item</code> from the database (to additionally check there is enough on sale, in case <code>Item</code> table in database changed and UI didn't update before user submitted order). <code>findOne</code> method I am calling for this purpose is implemented as follows:</p>
  
  <pre><code>@Repository
  public class GenericRepositoryImpl&lt;T, ID extends Serializable&gt; implements GenericRepository&lt;T, ID&gt; {
      @PersistenceContext
      EntityManager em;
  
      protected Class&lt;T&gt; entityClass;
  
      public GenericRepositoryImpl(Class&lt;T&gt; entityClass) {
          this.entityClass = entityClass;
      }
  
      @Override
      public T findOne(ID id) {
          return em.find(entityClass, id);
      }
  }
  </code></pre>
  
  <p>After the call <code>item</code> is not <code>null</code>, but it is not fetched from the database: I have EclipseLink logging turned on, at "FINEST" level, and there is no <code>SELECT</code> query. </p>
  
  <p>I thought that it is because EclipseLink has this <code>item</code> in its persistence context, so it doesn't execute the query. So I've tried adding <code>em.clear()</code> before <code>return</code> in <code>findOne</code> method implementation, but it didn't help.</p>
  
  <p>How do I actually get the fresh <code>item</code> from the database?</p>
  
  <p>Thank you in advance.</p>
  
  <p>P.S. I've tried querying explicitly in my <code>findOne</code> method, like so:</p>
  
  <pre><code>    @Override
      public T findOne(ID id) {
          T result = null;
          try {
              TypedQuery&lt;T&gt; query = em.createQuery("SELECT e FROM " + entityClass.getName() + " e", entityClass);
              result = query.getSingleResult();
          } catch (RuntimeException e) {
              e.printStackTrace();
          }
          return result;
      }
  </code></pre>
  
  <p>This way I get the exception: <code>java.lang.IllegalStateException: During synchronization a new object was found through a relationship that was not marked cascade PERSIST: ftcApp.model.Order@cf616dce</code>. I have no idea why, because I'm not doing <code>persist</code>.</p>