将SQL查询转换为Hibernate查询的问题

时间:2016-05-26 03:23:39

标签: java mysql sql join hql

我是一个Hibernate新手,我需要将我的SQL查询转换为Hibernate格式:

SELECT pr.id as id,
        pr.name as name,
        pr.brand as brand,
        pr.age as age,
        pi.image_url as imageUrl
    FROM product pr
        JOIN product_image pi ON
            (pr.id=pi.id and pi.image_url IN 
                (SELECT t0.image_url
                    FROM product_image AS t0
                        LEFT JOIN product_image AS t1 ON
                            (t0.id = t1.id AND
                                t1.image_order > t0.image_order)
                                    WHERE t0.image_url IS NOT NULL))

此查询正在运行并获得所需的输出,但我必须将其转换为Hibernate格式。

我创建了一个ProductProductImage实体类,Set类中有一个ProductImage Product,我正在使用{{ 1}}。我想为每个产品显示一个图像,为此,我提取的图像最高:fetchType.LAZY。请帮我转换我的查询到Hibernate格式。提前致谢

我的实体课程:

产品:

preference(image_order)

ProductImage:

@Entity
@Table(name="product")
public class Product  implements java.io.Serializable {
    private Integer id;
    private String name;
    private int age;
    private String brand;
    private String snippet;
    private Set<ProductDetail> productDetails = new HashSet<>(0);
    private Set<ProductImage> productImages = new HashSet<>(0);
    //constructors, setters and getters
}

产品详情:

@Entity
@Table(name="product_image")
public class ProductImage  implements java.io.Serializable {
    private Integer id;
    private Product product;
    private String imageUrl;
    private Integer imageOrder;
    //constructors setters and getters
}

0 个答案:

没有答案