Hi in my application i have few small tables and i cache them All at the server startup in a Map. I want to implement same functionality by Ecache + hibernate combination
my class looks like
@Entity
@Table(name = "Order")
@NamedQueries
({
@NamedQuery(name="Order.findBySource",
query="from Order a where a.source = :source"),
@NamedQuery(name="Order.findByAll",
query="from Order"),
@NamedQuery(name="Order.findByPrioritySource",
query="from Order a where a.priority = :priority and a.source = :source"),
@NamedQuery(name="Order.findByPriority",
query="from Order a where a.priority = :priority"),
@NamedQuery(name="Order.findByWhenModified",
query="from Order a where a.whenModified = :whenModified")
})
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Order implements java.io.Serializable {
private String source;
private long priority;
private Date whenModified;
}
this order table has hardly 10 entries and i want to keep this table in memory all the time.
any insights how can i achieve this?
i fire the above named queries to fetch the orders. I do not want to go to DB again and again when i fire this query so for this in ecache.xml do i need to make an entry of this class ? or i need to cache these Named queries in ecache.xml?
is it possible to replicate the Map implementaion by Ehcache. so that i can get the object just by specifying the ID from the cache without hitting the DB again and again ?
The thing you need is Hibernate query cache. First of all you need to enable it on Hibernate level by setting hibernate.cache.use_query_cache property to true. Then when you execute your query in the code you need to set a flag on the query object to indicate that it requires caching: