Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Explicit L2 cache overrides using EM and query hints
@Stateless
public class YetAnotherEmployeeManager {
@persistenceContext("emp_PU")
EntityManager em;
public Employee getByPrimaryKey(String employeeID){
Map<String,Object> searchProp = new HashMap<>();
searchProp.put("javax.persistence.cache.storeMode", CacheStoreMode.BYPASS); //don't put the employee into the L2 cache
return em.find(Employee.class, employeeID);
}
public Employee getByOrganization(String orgID){
TypedQuery<Employee> query = em.createQuery("SELECT e FROM Employees e WHERE e.orgID = :orgId", Employee.class);
query.setHint("javax.persistence.cache.storeMode", CacheRetrieveMode.REFRESH);
query.setParameter("orgId", orgId);
return query.getSingleResult();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment