我在Oracle中有兩個表,分別是product
和product_image
。顧名思義,他們必須從product
到product_image.
無法將每個組的最大N的SQL轉換爲HQL
的product
實體一個一對多的關係:
@Entity
@Table(name = "PRODUCT", catalog = "", schema = "WAGAFASHIONDB")
public class Product implements java.io.Serializable
{
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@Column(name = "PROD_ID", nullable = false, precision = 35, scale = 0)
@SequenceGenerator(name = "productIdSequence", sequenceName = "PRODUCT_SEQ", allocationSize=1, initialValue=1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "productIdSequence")
private Long prodId;
@Column(name = "PROD_NAME", length = 50)
private String prodName;
@Column(name = "PROD_CODE", length = 50)
private String prodCode;
@Lob
@Column(name = "PROD_DESC")
private String prodDesc;
@Basic(optional = false)
@Column(name = "MARKET_PRICE", nullable = true, precision = 35, scale = 2)
private BigDecimal marketPrice;
@Basic(optional = false)
@Column(name = "SALE_PRICE", nullable = true, precision = 35, scale = 2)
private BigDecimal salePrice;
@Column(name = "PROD_FEATURED")
private Short prodFeatured;
@Column(name = "EXPRESS_DELIVERY")
private Short expressDelivery;
@Basic(optional = false)
@Column(name = "PROD_WEIGHT", nullable = true, precision = 35, scale = 2)
private BigDecimal prodWeight;
@Column(name = "PROD_OCCASSION", length = 50)
private String prodOccassion;
@Basic(optional = false)
@Column(name = "QTY_AVAILABLE", nullable = true)
private BigInteger qtyAvailable;
@Column(name = "LATEST")
private Short latest;
@Column(name = "VISIBLE")
private Short visible;
@JoinTable(name = "PRODUCT_SIZE", joinColumns = {
@JoinColumn(name = "PROD_ID", referencedColumnName = "PROD_ID")}, inverseJoinColumns = {
@JoinColumn(name = "SIZE_ID", referencedColumnName = "SIZE_ID")})
@ManyToMany(fetch = FetchType.LAZY)
private Set<SizeTable> sizeTableSet;
@ManyToMany(mappedBy = "productSet", fetch = FetchType.LAZY)
private Set<Colour> colourSet;
@OneToMany(mappedBy = "prodId", fetch = FetchType.LAZY)
private Set<Measurement> measurementSet;
@OneToMany(mappedBy = "prodId", fetch = FetchType.LAZY)
private Set<Wish> wishSet;
@OneToMany(mappedBy = "prodId", fetch = FetchType.LAZY)
private Set<Cart> cartSet;
@OneToMany(mappedBy = "prodId", fetch = FetchType.LAZY)
private Set<ProductImage> productImageSet; //<--------
@OneToMany(cascade = CascadeType.ALL, mappedBy = "product", fetch = FetchType.LAZY)
private Set<OrderItem> orderItemSet;
@JoinColumn(name = "SUB_CAT_ID", referencedColumnName = "SUB_CAT_ID")
@ManyToOne(fetch = FetchType.LAZY)
private Subcategory subCatId;
@JoinColumn(name = "FABRIC_ID", referencedColumnName = "FABRIC_ID", nullable = false)
@ManyToOne(optional = false, fetch = FetchType.LAZY)
private Fabric fabricId;
@JoinColumn(name = "BRAND_ID", referencedColumnName = "BRAND_ID")
@ManyToOne(fetch = FetchType.LAZY)
private Brand brandId;
@OneToMany(mappedBy = "prodId", fetch = FetchType.LAZY)
private Set<Inquiry> inquirySet;
@OneToMany(mappedBy = "prodId", fetch = FetchType.LAZY)
private Set<Rating> ratingSet;
}
的ProductImage
實體:
@Entity
@Table(name = "PRODUCT_IMAGE", catalog = "", schema = "WAGAFASHIONDB")
public class ProductImage implements java.io.Serializable
{
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@Column(name = "PROD_IMAGE_ID", nullable = false, precision = 35, scale = 0)
@SequenceGenerator(name = "productIdSequence", sequenceName = "PRODUCT_IMAGE_SEQ", allocationSize=1, initialValue=1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "productIdSequence")
private Long prodImageId;
@Lob
@Column(name = "PROD_IMAGE")
private String prodImage;
@JoinColumn(name = "PROD_ID", referencedColumnName = "PROD_ID")
@ManyToOne(fetch = FetchType.LAZY)
private Product prodId; //<--------------
}
我需要獲取的資料列ProductImage
的每組行(實際上每組產品)的最大值prodId
,即每組最大的N個。
Oracle本機SQL可按如下方式構建。
SELECT pi.prod_image_id, pi.prod_id, pi.prod_image
FROM product_image pi
INNER JOIN (select max(pi.prod_image_id) AS prod_image_id, pi.prod_id FROM product_image pi GROUP BY pi.prod_id) prod_image
ON pi.prod_image_id=prod_image.prod_image_id
這與預期的完全相同,但我無法將此SQL轉換爲HQL。試圖直接在createQuery()
方法中作爲HQL執行時會導致QuerySyntexException
,第一個圓括號處的意外令牌。您能否給我一個提示將此SQL轉換爲HQL?
編輯:
一些問題,如下面還沒有回答做到這一點,因爲它實際上意味着:
- Best way to use hibernate for complex queries like top n per group
- hql query to retrieve top n from each group
- Using HQL with MySQL how can I order the result set before the group by so the right record is picked?
我想,在Hibernate中通過HQL無法實現這一點。我可能需要使用某些DTO類將此SQL分解爲適當的兩個或多個HQL語句。如果可以通過像上面的SQL這樣的單個HQL語句,那麼請不要忘記在這裏回答,因爲我在和我的應用程序中有很多地方有這樣的要求。
這確實有效。實際上,我修剪了SQL以減少複雜性。只有當此HQL檢索到的結果集可以進一步與'product'表類似的SQL'SELECT p。*,t.prod_image,t.prod_image_id FROM product p LEFT JOIN(select pi .prod_image_id,pi.prod_id,pi.prod_image FROM product_image pi INNER JOIN(SELECT max(pi.prod_image_id)AS prod_image_id,pi.prod_id from product_image pi group by pi.prod_id)prod_image ON pi.prod_image_id = prod_image.prod_image_id)t ON p.prod_id = t.prod_id ORDER BY p.prod_id DESC;'。 – Tiny 2013-04-07 05:59:34
這似乎不可能。這可以成爲可能嗎?非常感謝。 – Tiny 2013-04-07 06:00:34
我認爲是的。只需將'Product'添加到外部'select',HQL支持左連接。 – lunr 2013-04-07 16:51:44