How can i add complete Entity with Nested Entities in Projection.
The Problem is I want to convert the given HQL to Criteria API.
SELECT p FROM Product p LEFT JOIN p.reviews r GROUP BY p ORDER BY r.rating ASC
Consider 1 Product has many Reviews. (One To Many Relationship)
Also I am reluctant to using Projection as Product class has other entities which are fetched eagerly. eg Product class has Brand Entity. (Many To One).
If possible I want minimum amount of Projection code.
Product class
@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;
}
}
Review
@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;
}
}