0

我在我的DAO類中有下面的方法。在Tridion 2011中無法找到Hibernate查詢中的命名參數

public PublishAction findbyLatestPublishedDate(int tcmURI,int pubID) throws StorageException 
{ 
    log.info("Entering Method: JPAPublishActionDAO.PublishAction.findbyLatestPublishedDate"); 
    StringBuilder queryBuilder = new StringBuilder(); 
    queryBuilder.append("select pa from PublishAction pa where pa.id in(select pb.id from PublishAction pb where pb.ITEM_REFERENCE_ID=:tcmURI and pb.PUBLICATION_ID=:pubID and rownum <= 1 order by pb.LAST_PUBLISHED_DATE desc)"); 

    Map<String, Object> queryParams = new HashMap<String, Object>(); 
    queryParams.put("ITEM_REFERENCE_ID", tcmURI); 
    queryParams.put("PUBLICATION_ID", pubID); 

    log.debug("JPAPublishActionDAO findbyLatestPublishedDate -> queryBuilder- "+ queryBuilder.toString());   
    return executeQuerySingleResult(queryBuilder.toString(), queryParams); 
} 

這是給下面的錯誤:

2013-01-16 12:17:01,381 ERROR DeployPipelineExecutor - Original stacktrace for transaction: tcm:0-5607662-66560 
java.lang.IllegalArgumentException: org.hibernate.QueryParameterException: could not locate named parameter [PUBLICATION_ID] 

這是查詢內部在我的日誌受HQL產生:

2013-01-16 12:17:01,365 DEBUG QueryTranslatorImpl - SQL: select publishact0_.ID as ID66_, publishact0_.ITEM_REFERENCE_ID as ITEM2_66_, publishact0_.LAST_PUBLISHED_DATE as LAST3_66_, publishact0_.PUBLICATION_ID as PUBLICAT4_66_, publishact0_.ACTION as ACTION66_, publishact0_.FLAG as FLAG66_, publishact0_.ITEM_TYPE as ITEM7_66_, publishact0_.SCHEMA_ID as SCHEMA8_66_, publishact0_.URL as URL66_ from AUTN_ITEMS publishact0_ where publishact0_.ID in (select publishact1_.ID from AUTN_ITEMS publishact1_ where publishact1_.ITEM_REFERENCE_ID=? and publishact1_.PUBLICATION_ID=? and rownum<=1 order by publishact1_.LAST_PUBLISHED_DATE desc) 

我可以看到PUBLICATION_ID列在我的實體存在,以及如在我的SQL表中。

請建議。

回答

4

有關JPQL(或HQL)的美妙之處在於,您可以在查詢中使用Java類的屬性。您不應該將JPQL語法與純SQL語法組合起來。您應該將pb.ITEM_REFERENCE_ID=:tcmURI,pb.PUBLICATION_ID=:pubIDpb.LAST_PUBLISHED_DATE替換爲pb.tcmuri=:tcmURIpb.publicationId=:pubIDpb.last_published_date。除此之外,您的參數映射應該包含tcmURI和pubID,而不是您在其中放置的內容。請注意,我用實體中的實際字段替換了數據庫中的列。作爲一個結論,你的方法應該是這個樣子:

queryBuilder.append("select pa from PublishAction pa where pa.id in(select pb.id from PublishAction pb where pb.tcmuri=:tcmURI and pb.publicationId=:pubID and rownum <= 1 order by pb.last_published_date desc)"); 

Map<String, Object> queryParams = new HashMap<String, Object>(); 
queryParams.put("tcmURI", tcmURI); 
queryParams.put("pubID", pubID); 

或者你可以分割你的查詢在2這樣的:

StringBuilder queryBuilder = new StringBuilder(); 
queryBuilder.append("from PublishAction pb where pb.tcmuri=:tcmURI and pb.publicationId=:pubID order by pb.last_published_date desc"); 

Map<String, Object> queryParams = new HashMap<String, Object>(); 
queryParams.put("tcmuri", tcmURI); 
queryParams.put("pubID", pubID); 

final List<PublishAction> myActions = super.executeQueryListResult(queryBuilder.toString(), queryParams, 1); 

if (myActions != null && !myActions.isEmpty()) { 
    StringBuilder queryBuilderFinal = new StringBuilder(); 
    queryBuilderFinal.append("select pa from PublishAction pa where pa.id=:myId"); 

    Map<String, Object> queryParamsFinal = new HashMap<String, Object>(); 
    queryParamsFinal.put("myId", myActions.get(0).getId()); 

    return super.executeQuerySingleResult(queryBuilderFinal.toString(), queryParamsFinal) 
} 

請注意,我只能猜測是什麼的名字屬性在你的Java類中,所以我只是猜測你有tcmuri,publicationIdlast_published_date屬性。

希望這會有所幫助。

+0

嗨丹尼爾,感謝您的回覆現在我得到wiered錯誤的訂單子句說com.microsoft.sqlserver.jdbc.SQLServerException:在視圖,內聯函數,派生表,子查詢和公用表ORDER BY子句無效表達式,除非TOP或FOR XML也被指定。 –

+0

這並不像看起來那麼簡單。解決它的最簡單方法是將查詢分割爲2:「從PublishAction pb中選擇pb.id,其中pb.tcmuri =:tcmURI和pb.publicationId =:pubID order by pb.lastPublishDate desc」and seconde one「select pa從PublishAction pa where pa.id =:myId「,其中myId從第一個查詢開始。 –

+0

嗨你能否請更新你的ansewer中的示例代碼,因爲它不太清楚你的意見 –

相關問題