views:

220

answers:

1

I am setting up Hibernate Caching and want to cache certain entities in different regions. For example, some entities can be "stale" for up to 5 minutes, some for an hour, and some (like lookups) won't change for weeks. To facilitate easy config of regions in my code, I'm trying the following:

I created an annotation named @LookupCache (and @DailyCache etc)

@Cache(region = "lookups", usage = CacheConcurrencyStrategy.READ_ONLY)
@Retention(RetentionPolicy.RUNTIME)
public @interface LookupCache {}

And I am adding that annotation to my Hibernate/JPA entity:

@LookupCache
public class Course {}

This way, I can easily change the region or attributes of the @LookupCache without having to change the annotation params of every class.

However, the cache loader doesn't pick up this inherited @Cache notation. How do I get the @LookupCache annotation to inherit the annotations that are applied to it?

Update: To clarify, the @Cache annotation is a built-in hibernate annotation used by second-level caches like EHCache. I can't modify the @Cache annotation to make it inheritable by other annotations (my client doesn't want to maintain a special fork of hibernate). This is probably my only option though.

+1  A: 

Here is a simple example that shows how to retrieve details of the @Cache annotation applied to your @LookupCache annotation:

Course c = new Course();
LookupCache lookupCache = c.getClass().getAnnotation(LookupCache.class);
Cache cache = lookupCache.annotationType().getAnnotation(Cache.class);
System.err.println("region " + cache.region());
System.err.println("usage " + cache.usage());

You just need to make sure that @Cache annotation also has @Retention(RetentionPolicy.RUNTIME)

Eugene Kuleshov