Saturday, February 6, 2010

EJB with EclipseLink: How to ensure that your query returns fresh data

If you wanted to retrieve always fresh data from your query, you could set the hints parameter in the @NamedQuery declaration like below:
import org.eclipse.persistence.config.HintValues;
import org.eclipse.persistence.config.QueryHints;

@NamedQuery(name = "findEmployeesByDepartmentId", 
            query = "select o from Employee o where = :departmentId"), 
            hints = {@QueryHint(name=QueryHints.REFRESH, value=HintValues.TRUE)}

It could also be set programmatically thru the setHints() method of the Query API. An example would be:
public List findEmployeesByDepartmentId(Long departmentId){
     Query query = em.createNamedQuery("findEmployeesByDepartmentId");
     query.setParameter("departmentId", departmentId);
     query.setHint(QueryHints.REFRESH, HintValues.TRUE);
     return query.getResultList();

No comments:

Post a Comment