【问题标题】:Add Complete Entity with Nested Entities in Projection在 Projection 中添加具有嵌套实体的完整实体
【发布时间】:2017-02-26 02:21:53
【问题描述】:

如何在 Projection 中添加带有嵌套实体的完整实体。

问题是我想将给定的 HQL 转换为 Criteria API。

SELECT p FROM Product p LEFT JOIN p.reviews r GROUP BY p ORDER BY r.rating ASC

考虑 1 个产品有很多评论。 (一对多关系)

我也不愿意使用 Projection,因为 Product 类还有其他急切获取的实体。例如,产品类具有品牌实体。 (多对一)。

如果可能,我想要最少数量的投影代码。

产品类别

@Entity
public class Product {

    @Id
    @GeneratedValue
    private long id;

    private String name;

    @OneToMany(mappedBy = "product")
    private Review review;

    @ManyToOne
    private Brand brand;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Review getReview() {
        return review;
    }

    public void setReview(Review review) {
        this.review = review;
    }

    public Brand getBrand() {
        return brand;
    }

    public void setBrand(Brand brand) {
        this.brand = brand;
    }

}

评论

@Entity
public class Review {

    @Id
    @GeneratedValue
    private long id;

    private String text;

    private int rating;

    @ManyToOne
    private Product product;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public int getRating() {
        return rating;
    }

    public void setRating(int rating) {
        this.rating = rating;
    }

    public Product getProduct() {
        return product;
    }

    public void setProduct(Product product) {
        this.product = product;
    }

}

【问题讨论】:

  • @NeilStockton 据我所知,我可以使用 Projections.groupProperty("product.id") 和 Projections.avg("review.rating") 然后我可以按平均评论排序。但我将不得不在投影中添加每个字段。我的要求很简单,我想要产品列表,并希望按它们的平均评分来订购它们。 1 产品可能有很多评分
  • 在这种情况下,这不是 JPA Criteria API,因此删除 JPA 标记。
  • @NeilStockton 好的。你能帮我解决上述 HQL 的休眠标准吗?或者如果 JPA 标准是可能的,那么它也会有帮助。

标签: hibernate hql hibernate-criteria


【解决方案1】:

如果您的问题出在插入上,则必须在父类上配置级联(CascadeType)。 http://docs.oracle.com/javaee/6/tutorial/doc/bnbqa.html

如果您需要查询,这可以作为 join+order 查询的示例:

CriteriaQuery<Product> cq = cb.createQuery(Product.class);
Root<Product> product= cq.from(Product.class);
Join<Product, Review> review = product.join("review");
cq.select(product);
cq.groupBy(review.get("rating"));
cq.orderBy(cb.asc(cb.avg(review.get("rating"))));

来源:http://docs.oracle.com/javaee/6/tutorial/doc/gjivm.html

【讨论】:

  • 我想按平均评分排序
  • 我已经编辑了答案,因此它包含了 group by。无论如何,我认为有关 Using the Criteria API 的链接值得一看。
  • 谢谢,我正在使用 Hibernate。我有 Hibernate SessionFactory 实例。我可以从中获取 EntityManager 实例或 Criteria builder Instance。如果不是,您可以修改 Hibernate Criteria 的代码吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-10-07
  • 1970-01-01
  • 2020-11-28
  • 2021-08-18
  • 2021-07-28
  • 1970-01-01
  • 2021-12-04
相关资源
最近更新 更多