Hibernate嵌套选择子查询

时间:2016-10-05 12:55:21

标签: hibernate subquery hibernate-criteria

我需要制定一个将调用此SQL查询的Hibernate标准

SELECT *, (SELECT avg(rating) FROM restaurant_review WHERE restaurant_id = restaurant.id) as "avegrageRating" FROM restaurant;

请帮助:D

编辑: 以下是实体:

Restaurant.java:

@Entity
@Table(name = "restaurant")
public class Restaurant {

    @Id
    @Column(name = "id")
    public int id;

    @Column(name = "name")
    public String name;

    @Column(name = "description")
    public String description;

    @OneToMany(mappedBy = "restaurantId")
    public List<RestaurantReview> reviews;
}

RestaurantReview.java

@Entity
@Table(name = "restaurant_review")
public class RestaurantReview {

    @Id
    @Column(name = "id")
    public Integer id;

    // This Many to One causes an Unable to build entity manager factory exception
    @ManyToOne
    @JoinColumn(name = "restaurant_id", referencedColumnName="id")
    public Integer restaurantId;

    @Column(name = "user_id")
    public Integer userId;

    @Column(name = "rating")
    public Integer rating;
}

我希望这会有所帮助。 我需要在餐厅餐桌上获取所有餐厅的列表,其中一列包含其他餐桌的平均评级。

1 个答案:

答案 0 :(得分:0)

由于您在此处未提及您的实体,因此假设您的实体我已经制作了以下代码,请检查...希望它能帮到您。

/usr/include/x86_64/linux/sys/ptrace.h