How To Configure Query Cacheing in EclipseLink
        Posted  
        
            by rustyshelf
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by rustyshelf
        
        
        
        Published on 2009-02-25T04:53:52Z
        Indexed on 
            2010/05/26
            12:11 UTC
        
        
        Read the original article
        Hit count: 277
        
I have a collection of states, that I want to cache for the life of the application, preferably after it is called for the first time. I'm using EclipseLink as my persistence provider. In my EJB3 entity I have the following code:
@Cache
@NamedQueries({
    @NamedQuery(
        name = "State.findAll",
        query = "SELECT s FROM State s",
        hints = {
                @QueryHint(name=QueryHints.CACHE_USAGE, value=CacheUsage.CheckCacheThenDatabase),
                @QueryHint(name=QueryHints.READ_ONLY, value=HintValues.TRUE)
            }
    )
})
This doesn't seem to do anything though, if I monitor the SQL queries going to MySQL it still does a select each time my Session Bean uses this NamedQuery.
What is the correct way to configure this query so that it is only ever read once from the database, preferably across all sessions?
Edit: I am calling the query like this:
Query query = em.createNamedQuery("State.findAll");
List<State> states = query.getResultList();
© Stack Overflow or respective owner