2

I am using annotation based configuration to configure the cache strategy for all hibernate entities like as below

@Entity
@Table(name = "EMP")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class EMPLOYEE extends AbstractEntity { 

We have common project to maintain all hibernate entity clasess. we will building a jar and using it in different applications. We are using this entity jar in two applications. I have to use different caching strategies for two applications. For application1 , i have to use @Cache(usage = CacheConcurrencyStrategy.READ_WRITE) and for application 2, i have to use @Cache(usage = CacheConcurrencyStrategy.READ_ONLY).

So i would like to change these configuration settings dynamically at run time for application2.

Appreciated , if anyone can suggest the approach.

1 Answer 1

2

You don't have to change the caching policy for a class run-time. You need just to have a separate class with constants like this:

public abstract class Settings {
   public static final CacheConcurrencyStrategy CACHE_STRATEGY;

   static {
      // initialize the field, e.g. based on a value from a properties file
   }
}

then 

@Entity
@Table(name = "EMP")
@Cache(usage = Settings.CACHE_STRATEGY)
public class EMPLOYEE extends AbstractEntity {...}

Then just have two different files with properties for different applications.

P.S. it doesn't have to be a separate class with the constant. It just an example how things can be organized better, from my point of view.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.