3
使用JPA/Eclipse TopLink。使用JPQL createQuery返回的旧数据
我更新(使用createNativeQuery)的表如下所示:
Query query = em.createNativeQuery("UPDATE Products SET productName='" + p.getProductName() + "',productVendor='" + p.getProductVendor() + "',productDescription='" + p.getProductDescription() + "',quantityInStock=" + p.getQuantityInStock() + ",buyPrice =" + p.getBuyPrice() + ",msrp=" + p.getMsrp() + " WHERE productCode='" + p.getProductCode() + "'");
query.executeUpdate();
更新反映在DB(MySQL的)
对于如下显示检索(使用的createQuery):
Query query1 = em.createQuery("SELECT p from Products p where p.productCode='"+searchTerm+"'");
return query1.getResultList();
但是,返回的ResultList始终是更新前的数据。但是,当我使用createNativeQuery而不是createQuery时,返回最新的更新数据。 createQuery方法可能会出现什么错误?
感谢,JB Nizet。这是一个很好的解释。 使用em.clear()清除entitymanager缓存没有帮助。所以我最终绕过了缓存 - query.setHint(“javax.persistence.cache.storeMode”,“BYPASS”);它的工作。 (我明白绕过缓存的全部目的,正如你在文章中提到的那样,将避免使用批量更新和原生查询)。再次感谢。 – srock