2013-10-10 152 views
2

Iam使用EclipseLink的條件查詢作爲JPA。我需要根據參數緩存查詢結果。當我使用query.setHint("eclipselink.QUERY_RESULTS_CACHE", "TRUE")時,它仍然觸發對數據庫的查詢。我如何在標準查詢的上下文中使用它?條件查詢緩存在Eclipselink緩存?

在這裏,我把我的查詢和他們的提示和addNamedQuery

Query query = psEntityManager.getEntityManager().createQuery(criteriaQuery);// Creating Query to supply Values 
     query.setHint("eclipselink.QUERY_RESULTS_CACHE", "TRUE"); 
     query.setHint(QueryHints.QUERY_TYPE,QueryType.ReadObject); 
     psEntityManager.getEntityManager().getEntityManagerFactory().addNamedQuery("query1", query); 

這裏是我的輸出:

Query1----------- 
[EL Fine]: sql: 2013-10-10 21:33:35.495--ServerSession(14144548)--Connection(7200601)--SELECT PRIMARYKEY, ADDRESSLINE1, ADDRESSLINE2, PHONENUMBER, fkCityId, fkPersonId FROM ADDRESS WHERE (fkPersonId = ?) 
    bind => [2] 
[EL Fine]: sql: 2013-10-10 21:33:35.527--ServerSession(14144548)--Connection(7200601)--SELECT PRIMARYKEY, CITYNAME, PINCODE, fkStateId FROM CITY WHERE (PRIMARYKEY = ?) 
    bind => [2] 
[EL Fine]: sql: 2013-10-10 21:33:35.528--ServerSession(14144548)--Connection(7200601)--SELECT PRIMARYKEY, STATENAME FROM STATE WHERE (PRIMARYKEY = ?) 
    bind => [1] 
[EL Fine]: sql: 2013-10-10 21:33:35.531--ServerSession(14144548)--Connection(7200601)--SELECT PRIMARYKEY, AGE, DOB, FIRSTNAME, LASTNAME, SEX, TIMESTAMP, fkDepartmentId FROM PERSON WHERE (PRIMARYKEY = ?) 
    bind => [2] 
[EL Fine]: sql: 2013-10-10 21:33:35.541--ServerSession(14144548)--Connection(7200601)--SELECT PRIMARYKEY, DEPTNAME FROM DEPARTMENT WHERE (PRIMARYKEY = ?) 
    bind => [2] 
[EL Fine]: sql: 2013-10-10 21:33:35.547--ServerSession(14144548)--Connection(7200601)--SELECT PRIMARYKEY, CITYNAME, PINCODE, fkStateId FROM CITY WHERE (PRIMARYKEY = ?) 
    bind => [5] 
[EL Fine]: sql: 2013-10-10 21:33:35.548--ServerSession(14144548)--Connection(7200601)--SELECT PRIMARYKEY, STATENAME FROM STATE WHERE (PRIMARYKEY = ?) 
    bind => [3] 
[EL Fine]: sql: 2013-10-10 21:33:35.551--ServerSession(14144548)--Connection(7200601)--SELECT PRIMARYKEY, CITYNAME, PINCODE, fkStateId FROM CITY WHERE (PRIMARYKEY = ?) 
    bind => [8] 
[EL Fine]: sql: 2013-10-10 21:33:35.553--ServerSession(14144548)--Connection(7200601)--SELECT PRIMARYKEY, STATENAME FROM STATE WHERE (PRIMARYKEY = ?) 
    bind => [4] 
Query2----------- 
[EL Fine]: sql: 2013-10-10 21:33:35.557--ServerSession(14144548)--Connection(7200601)--SELECT PRIMARYKEY, ADDRESSLINE1, ADDRESSLINE2, PHONENUMBER, fkCityId, fkPersonId FROM ADDRESS WHERE (fkPersonId = ?) 
    bind => [2] 
Query3----------- 
[EL Fine]: sql: 2013-10-10 21:33:35.56--ServerSession(14144548)--Connection(7200601)--SELECT PRIMARYKEY, ADDRESSLINE1, ADDRESSLINE2, PHONENUMBER, fkCityId, fkPersonId FROM ADDRESS WHERE (fkPersonId = ?) 
    bind => [2] 
Query4----------- 
[EL Fine]: sql: 2013-10-10 21:33:35.563--ServerSession(14144548)--Connection(7200601)--SELECT PRIMARYKEY, ADDRESSLINE1, ADDRESSLINE2, PHONENUMBER, fkCityId, fkPersonId FROM ADDRESS WHERE (fkPersonId = ?) 
    bind => [2] 
+0

@Chris請回答 – Achyut

+0

您是否將此查詢存儲爲命名查詢? JPA 2.1中的EMF提供addNamedQuery動態存儲命名查詢 – Chris

+0

@Chris否,此查詢是條件查詢 – Achyut

回答

1

取出query.setHint(QueryHints.QUERY_TYPE,QueryType.ReadObject),或致電它首先。 query_type更改底層查詢對象,並非所有提示都可能被複制到新對象中。反正似乎沒有必要。

+0

刪除後無效。每當我使用criteia創建查詢時,我正在使用這個代碼塊。 – Achyut

+0

你沒有顯示你多次執行查詢的方式。 – Chris

+0

我的意思是,每個查詢Iam使用條件生成器創建查詢,並使用上述代碼塊,然後使用getResult。 – Achyut