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

ここで、メソッドに基づいて上記の表に基づいて、休止状態のクエリを使用して 1 つのレコードを返すメソッドを書きたいuriのでpid、以下のようになります。

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

テーブルの構造についてはよくわかりませんが、データベースを2回呼び出す必要があります。

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 に答える
1

rownum休止状態の句を使用してみて、適用すると、トップから 1 つの結果のみを取得できます。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 に答える