Typically, I use Hibernate @Cache (using = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE) to cache the @Entity class, and it works well.
There is another @Cacheable annotation in JPA2 that seems to match functionality with Hibernate @Cache. To make the entity class independent of the hibernate package, I want to try. But I canβt make it work. Every time a simple id request still ends up in the database.
Can someone tell me where it goes wrong? Thank.
Entity Class:
@Entity //@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE) @Cacheable(true) public class User implements Serializable { // properties }
Testing Class:
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations={"classpath:app.xml"}) @TransactionConfiguration(transactionManager="transactionManager") public class UserCacheTest { @Inject protected UserDao userDao; @Transactional @Test public void testGet1() { assertNotNull(userDao.get(2L)); } @Transactional @Test public void testGet2() { assertNotNull(userDao.get(2L)); } @Transactional @Test public void testGet3() { assertNotNull(userDao.get(2L)); } }
The test result shows each access level "get" (with hibernate.show_sql = true).
Persistence.xml:
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/> <property name="hibernate.show_sql" value="true"/> <property name="hibernate.format_sql" value="true" /> <property name="hibernate.use_outer_join" value="true"/> <property name="hibernate.cache.provider_class" value="org.hibernate.cache.SingletonEhCacheProvider"/> <property name="hibernate.cache.use_second_level_cache" value="true"/> <property name="hibernate.cache.use_query_cache" value="true"/>
JPA Code:
@Override public T get(Serializable id) { return em.find(clazz, id); }
smallufo Sep 08 '10 at 1:20 2010-09-08 01:20
source share