私の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);
}