Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7517069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T01:19:49+00:00 2026-05-30T01:19:49+00:00

I am using Spring 3.1 Cache using EhCache currently to implement method caching. Consider

  • 0

I am using Spring 3.1 Cache using EhCache currently to implement method caching. Consider the code fragment below:

@Cacheable("items")
public Item findByPK(int itemID) {
    String sql = "SELECT * FROM ITEM WHERE ITEM_ID = ?";
    Item item = getJdbcTemplate().queryForObject(sql, new Object[]{itemID}, new ItemRowMapper());
    return item;
}

@Cacheable("items")
public List<Item> findAll() {
    String sql = "SELECT * FROM ITEM";
    List<Item> items = getJdbcTemplate().query(sql,new ItemRowMapper());
    return items;
}

If I call findByPK() it hits the database first, then onwards it hits the cache, so the method caching works. findAll() ditto. However, is there any way to instruct spring to make findByPK() calls recognize the results returned by findAll()?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T01:19:51+00:00Added an answer on May 30, 2026 at 1:19 am

    This is a major hack, but it would give you the functionality you want:

    @Cacheable("items")
    public Item findByPK(int itemID) {
        String sql = "SELECT * FROM ITEM WHERE ITEM_ID = ?";
        Item item = getJdbcTemplate().queryForObject(sql, new Object[]{itemID}, new ItemRowMapper());
        return item;
    }
    
    @Cacheable("items")
    public List<Item> findAll() {
        String sql = "SELECT * FROM ITEM";
        List<Item> items = getJdbcTemplate().query(sql,new ItemRowMapper());
        for (Item item : items){
            removeThenAddToCache(item.getID(), item);
        }
        return items;
    }
    
    @CacheEvict(value = "items", key="#itemID")
    public void removeThenAddToCache(int itemID, Item item) {
        addToCache(item);
    }
    
    @Cacheable(value = "items", key="#itemID")
    public Item addToCache(int itemID, Item item) {
        return item;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

BAckground: I'm using ehcache as a cache implementation in a Spring 3.1 application. I'm
I am using java annotation based configuration for initializing ehcache based caching, with Spring
I'm using Spring.net with NHiberante (HibernateTemplate) to implement my DAO's. I also have some
i specified the below properties in cfg file. <property name=hibernate.cache.use_second_level_cache>true</property> <property name=hibernate.cache.provider_class> net.sf.ehcache.hibernate.EhCacheProvider</property> In
I'm using Spring 3.1 and I want to use the new cache features. Then,
I'm considering to use to implement a cache either JBoss Cache or Ehcache. After
In my main() method I create a PersonCollection Object using Spring and then I
Does spring security have a way to prevent the last point below? I'm using
I am using EhCache 1.4.0, Spring 3.0.5 in a web application deployed on Tomcat
I'm using grails 2.0.0.M1 with few plugins (spring-security-core-1.2.1, spring-cache-1.3.1). No problems cleaning and comipling

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.