Entity loaded from database despite being available in Hibernate's 2nd level cache
Issue
Hi,
Customer has enabled both query and entity caching in their application. However for a single entity (VehicleProperty) it hits the database on every call even though it is available in the cache. The application is deployed on JBoss EAP 5.1.2 and uses JPA with JBossCache.
I have created a test case to demonstrate the issue. Deploy the attached application and go to http://localhost:8080/jpatest/. Clicking on the button runs a named query to find an instance of VehicleProperty entity. On every run this query loads the entity from the database and puts it in cache. This can be verified in the server logs for every request:
15:17:19,419 INFO [STDOUT] Hibernate: /* load com.test.model.VehicleProperty */ select vehiclepro0_.name as name38_0_, vehiclepro0_.vehicle_id as vehicle3_38_0_, vehiclepro0_.value as value38_0_ from VehicleProperty vehiclepro0_ where vehiclepro0_.name=? and vehiclepro0_.vehicle_id=?
15:17:25,858 INFO [STDOUT] Hibernate: /* load com.test.model.VehicleProperty */ select vehiclepro0_.name as name38_0_, vehiclepro0_.vehicle_id as vehicle3_38_0_, vehiclepro0_.value as value38_0_ from VehicleProperty vehiclepro0_ where vehiclepro0_.name=? and vehiclepro0_.vehicle_id=?
15:17:26,034 INFO [STDOUT] Hibernate: /* load com.test.model.VehicleProperty */ select vehiclepro0_.name as name38_0_, vehiclepro0_.vehicle_id as vehicle3_38_0_, vehiclepro0_.value as value38_0_ from VehicleProperty vehiclepro0_ where vehiclepro0_.name=? and vehiclepro0_.vehicle_id=?
I have attached the server log with trace enabled for hibernate and jbosscache. After a few runs, multiple entry for the VehicleProperty instance are available in the cache:
--- Cache1 ---
/ null
/persistence.unit:unitName=jpatest.war#bookingDatabase null
/org null
/hibernate null
/cache null
/StandardQueryCache null
/QUERY null
/NODE null
/sql: select vehiclepro0_.name as name0_, vehiclepro0_.vehicle_id as vehicle3_0_, vehiclepro0_.value as value0_ from VehicleProperty vehiclepro0_ where vehiclepro0_.vehicle_id=? and vehiclepro0_.name=?; parameters: ; named parameters: {name=color, vehicle=1}; max rows: 2; transformer: org.hibernate.transform.CacheableResultTransformer@110f2 {item=[13744958278, [Ljava.lang.Object;@4aa0f1c4]}
/com null
/test null
/wss null
/model null
/Vehicle null
/ENTITY null
/NODE null
/com.test.model.Vehicle#1 {item=CacheEntry(com.test.model.Vehicle)[A,A]}
/VehicleProperty null
/ENTITY null
/com.test.model.VehicleProperty#VehiclePropertyPK [vehicle=1, name=color] {item=CacheEntry(com.test.model.VehicleProperty)[red]}
/com.test.model.VehicleProperty#VehiclePropertyPK [vehicle=1, name=color] {item=CacheEntry(com.test.model.VehicleProperty)[red]}
/com.test.model.VehicleProperty#VehiclePropertyPK [vehicle=1, name=color] {item=CacheEntry(com.test.model.VehicleProperty)[red]}
/com.test.model.VehicleProperty#VehiclePropertyPK [vehicle=1, name=color] {item=CacheEntry(com.test.model.VehicleProperty)[red]}
/NODE null
Environment
- JBoss Enterprise Application Platform (EAP)
- 5.x
Subscriber exclusive content
A Red Hat subscription provides unlimited access to our knowledgebase of over 48,000 articles and solutions.
Welcome! Check out the Getting Started with Red Hat page for quick tours and guides for common tasks.
