Hi,
I persist a new object into database through Stateless / Stateful EJB (tried with both of them), and then try to select all objects through cacheable query, it does not retrieve objects from the cache. But if i do not insert anything and just select, everything works fine. I use JBoss Cache for L2 caching, is it a normal behavior?
Here is the code i use.
Code:
@NamedQueries({@NamedQuery(query = "from Event",
name = "Event.findAll",
hints = {@QueryHint(name = "org.hibernate.cacheable", value = "true"),
@QueryHint(name="org.hibernate.cacheRegion", value = "EventRegion")
}
)
}
)
@Entity
@Table(name = "EVENT")
@Cache (usage=CacheConcurrencyStrategy.TRANSACTIONAL)
public class Event implements Serializable {
..
..
}
Methods in EJBCode:
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public Event persist(Event event) {
em.persist(event);
return event;
}
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public List<Event> findAllByRange(int start, int max) {
Query q = em.createNamedQuery("Event.findAll");
q.setFirstResult(start);
q.setMaxResults(max);
return q.getResultList();
}
And the test code:Code:
EventBean eventBean = new EventBean();
Event event = new Event();
event.setOwner(user);
event.setMessage(message);
event = eventBean.persist(event);
List<Event> eventList = eventBean.findAllByRange(0, 15);
Thanks