After reading lot of posts i am planning to use below approach to pull reference data from database (code not tested yet).
@Override
protected Map referenceData(HttpServletRequest request) throws Exception {
//Data referencing for country list box
Map referenceData = new HashMap();
referenceData.put("countryList", articleService.getCountryList());
//Data referencing for skills list box
Map<String,String> javaSkill = new LinkedHashMap<String,String>();
referenceData.put("skillsList", articleService.getSkillsList());
return referenceData;
}
Reference data table is as below
id key value type
-----------------------------------------
1 1 United States countries
2 2 India countries
3 3 United Kingdom countries
4 4 China countries
5 1 Java skills
6 2 Spring skills
7 3 Hibernate skills
8 4 SQL skills
I have got two questions,
- This is a static reference data (i wont change it for months), so I dont want to hit database for every request and data should be loaded on server startup instead. How to do this?
- Is this the correct approach or i am complately wrong in understanding it?
No, I would say it’s a correct approach. You should put countries in their own table, and skills in their own table.
First because additional column will inevitably appear for countries, which don’t make any sense for skills (or vice-versa).
And second because you will want to reference countries or skills from other tables in your database (and other entities in your object model). So you’ll want to have foreign keys from addresses to countries, for example, and you wouldn’t want an address to reference a skill rather than a country. And you’ll want a ManyToOne association from Address to Country, and not from Address to ReferenceTable.
I have the feeling that you’re pre-optimizing. Such reference tables are small, and the database will hold everything in memory. So querying them, even if you do it frequently, will be extreely fast.
If you want to avoid those queries completely, then you could simply use the second-level cache of hibernate, which will make everything transparent (i.e. you will keep programming as usual, and Hibernate will go to its cache instead of going to the database). This will bring additional advantages, such as being able to actually update information in those tables without getting stale values for too long (or without getting stale values at all).