0

我的 DAO 类中有以下方法。

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

1 回答 1

4

JPQL(或 HQL)的美妙之处在于您可以在查询中使用 Java 类的属性。您不应将 JPQL 语法与普通 SQL 语法结合使用。您应该将pb.ITEM_REFERENCE_ID=:tcmURI,pb.PUBLICATION_ID=:pubIDpb.LAST_PUBLISHED_DATE替换为pb.tcmuri=:tcmURI,pb.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属性。

希望这可以帮助。

于 2013-01-16T09:15:49.560 回答