我的 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
现在我想编写一个方法,该方法将在上表的基础上使用休眠查询返回一条记录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);
}