Thursday, May 16, 2013

Query to return fresh data


import org.eclipse.persistence.config.HintValues;
import org.eclipse.persistence.config.QueryHints;
...
@NamedQuery(name = "findSamplesById", 
query = "select s from Sample s where s.sample.id = :sampleId"), 
hints = {@QueryHint(name=QueryHints.REFRESH, value=HintValues.TRUE)}

Programmatical method

public List<Sample> getSamplesById() {
        try {
            Query query = em.createNamedQuery("findSamplesById");
            query.setParameter("sampleId", sampleId);
            query.setHint(QueryHints.REFRESH, HintValues.TRUE);
            return query.getResultList();
        } catch (Exception e) {
            log.error("getSamplesById() failed: " + e.getMessage());
            e.printStackTrace(System.err);
        }
 return null;
}