Hi,
I have multiple entities which contain a list of comments. Therefore each entity has an one-to-many unidirectional relationship to the entity class of Comment. The code below is simplified version of origin code.
Code:
@Entity
public class Sample implements Serializable {
/**
*
*/
private static final long serialVersionUID = -5363239412291896200L;
@Id
@GeneratedValue(generator = "system-uuid")
@GenericGenerator(name = "system-uuid", strategy = "uuid")
@Column(length = 32)
private String id;
private String title;
@OneToMany(cascade = { CascadeType.ALL }, fetch = FetchType.LAZY)
@JoinTable(name = "Sample_Comment", joinColumns = @JoinColumn(name = "sampleId"), inverseJoinColumns = @JoinColumn(name = "commentId"))
private List<Comment> comments;
}
Code:
@Entity
public class Comment implements Serializable {
/**
*
*/
private static final long serialVersionUID = 6519392048862951415L;
@Id
@GeneratedValue(generator = "system-uuid")
@GenericGenerator(name = "system-uuid", strategy = "uuid")
@Column(name = "id", length = 32)
private String id;
private String approvalState;
private String name;
private String message;
private Date date;
}
Each entity can have hundreds of comments. To reduce the payload of entity Sample, the comments will be lazy-loaded. Additionally, most of the time only approved comments (approvalState = approved) should be displayed. Therefore I need a query to load the paged and filter list of comments.
Currently, I'm using a HQL query with an inner join, but I like to use Criteria to fetch the comments.
Code:
select comments from Sample sample inner join sample.comments as comments where sample.id = :sampleId and comments.approvalState = :approvalState order by comments.date desc
So, I'm searching for a possibility using the Criteria API to return the approved comments of a single entity Sample as a paged list.
Patrick