-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Criteria based fetch of one-to-many associations
PostPosted: Thu Feb 19, 2015 7:20 pm 
Newbie

Joined: Thu Feb 19, 2015 4:25 pm
Posts: 1
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


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.