1

我的 MS SQL 2008 中有下表

我的表

----------------------
ID int (identity), 
action varchar(20),
uri int,
pid int,
url varchar(20),
lastpubdate date,
flag varchar(20)

以下是表中的一些示例记录:

ID      action      uri     pid     url                 lastpubdate         flag
---------------------------------------------------------------------------------
1       ADD         123     233     /en/index.aspx      15 JAN 2013:09:44   NULL
2       UPD         123     233     /en/index.aspx      15 JAN 2013:10:00   NULL
3       UPD         123     233     /en/index.aspx      15 JAN 2013:10:15   NULL
4       DEL         123     233     /en/index.aspx      15 JAN 2013:10:17   NULL

现在我想编写一个方法,该方法将在上表的基础上使用休眠查询返回一条记录uripid赋予方法,如下所示:

public MyTable findByLastPubDate(String uri,int pid)
{
    log.info("Entering Method: MyTable.findByLastPubDate");
    StringBuilder queryBuilder = new StringBuilder();
    queryBuilder.append("select top 1 from MyTable pa where pa.publication_id = :pid and  pa.uri = :uri and pa.flag IS NULL order by pa.last_published_date DESC");

    Map<String, Object> queryParams = new HashMap<String, Object>();
    queryParams.put("publication_id", pid);
    queryParams.put("tcmUri", uri);

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

请建议上述方法是否正确或需要一些修改

编辑: 我想在 Hibernate 中实现以下查询

SELECT 
 pa.[ID]
      ,pa.[ACTION]
      ,pa.[PUBLICATION_ID]
      ,pa.[ITEM_REFERENCE_ID]
      ,pa.[ITEM_TYPE]
      ,pa.[LAST_PUBLISHED_DATE]
      ,pa.[URL]
      ,pa.[SCHEMA_ID]
      ,pa.[flag]
FROM [mytable] a
WHERE pa.ID IN
(SELECT TOP 1 b.id FROM [mytable] b WHERE b.[ITEM_REFERENCE_ID] = 342349 and
b.[PUBLICATION_ID] = 233
ORDER BY b.[LAST_PUBLISHED_DATE] DESC)

修改方法:

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.tcmUri = :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("publication_id", pubID);
        queryParams.put("tcmUri", tcmURI);

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

2 回答 2

1

您可以尝试使用rownumhibernate 子句,一旦应用order by

select <entity.columns_list> 
from Entity entity 
where < where_condition > AND rownum <= 1 

或者在你的情况下

select pa from MyTable pa 
where pa.publication_id = :pid and  pa.uri = :uri 
and pa.flag IS NULL and rownum <=1
order by pa.last_published_date DESC

编辑:

select pa from [mytable] pa
where pa.id in
(
    select b.id 
    from [mytable] b 
    where b.itemReferenceId = :itemReferenceId 
    and b.publicationId = :publicationId
    and rownum <= 1
    order by b.lastPublishedDate desc
)
于 2013-01-16T04:31:08.720 回答
1

我不确定您的表结构,但是您需要对数据库进行两次调用。

public PublishAction getLatestRecordonBasedID(int tcmURI,int pubID) throws StorageException
{
    StringBuilder queryBuilder = new StringBuilder();
    queryBuilder.append("from PublishAction pb where pb.ITEM_REFERENCE_ID = :tcmURI and pb.PUBLICATION_ID = :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 = 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 executeQuerySingleResult(queryBuilderFinal.toString(), queryParamsFinal);
    }
    else 
    {
        return null;
    }
}

您可以在谷歌搜索后尝试类似上面的解决方案。

于 2013-01-17T09:42:59.873 回答