【发布时间】:2013-11-23 06:44:16
【问题描述】:
我有这个 HQL 查询,它返回表中的所有产品。但我只想获得最新价格日期的产品,而不是旧的产品。我该怎么做。
String select = "SELECT p FROM ProductPricing p where p.productId.supplierId.id=:id"; // nested condition based on the FK productpricing -> product -> supplier
Query query = hibernateSession.createQuery(select);
query.setParameter("id", Integer.parseInt(dsRequest.getFieldValue("supplier").toString()));
List<ProductPricing> models = query.list();
for (int i = 0; i < models.size(); i++)
{
if (models.get(i).getProductId() != null)
{
models.get(i).getProductId().getProductCode();
models.get(i).getProductId().getBrandName();
models.get(i).getPurchasePrice();
System.out.println(models.get(i).getProductId().getBrandName());
}
}
我的 ProductPrice 表如下所示。现在我的这个 HQL 查询返回所有记录。但我只希望它返回价格日期最新的记录,即那些 productID 的 id 2 和 4
id priceDate purchasePrice salesPrice consumerPrice productID description
1 2012-11-15 00:00:00 1000 1200 1500 1 qwqw
2 2013-11-16 00:00:00 1600 1800 2100 1 new price
3 2012-10-15 00:00:00 1600 1500 1600 2 erere
4 2013-11-02 00:00:00 1600 1900 3400 2 new price
【问题讨论】:
-
您是否为最新价格维护单独的列
-
什么意思??我的 ProductPricing 表看起来像这个 productPricing (id BIGINT NOT NULL IDENTITY, priceDate DATETIME, purchasePrice FLOAT(53), salesPrice FLOAT(53), consumerPrice FLOAT(53), productID BIGINT,描述 NVARCHAR(255)
-
请帮忙,我将不胜感激
-
我有 SQL 就足够了。稍后我会给你 HQL