Hibernate version: Hibernate 3.2.6, Hibernate Annotations 3.3.1, Hibernate Entity Manager 3.3.2
Structure: There are users. Users can duel each other. In a duel, the users are mapped as the challenger and the challengee. In the User bean, there are various collections of duels, such as receivedDuels (mapped by challengee) and initiatedDuels (mapped by challenger). There are scores associated with duels, mapped as challengerScore and challengeeScore. There are also collections of duels in the User bean that include @Where annotations to add conditions relating to the challengerScore or challengeeScore. The particular collection at issue is:
Code:
@OneToMany(cascade = CascadeType.ALL, mappedBy = "challenger")
@LazyCollection(LazyCollectionOption.EXTRA)
@Where(clause="challenger_score_id IS NOT NULL AND challengee_score_id IS NULL")
private Collection<Duel> duelsAwaitingResponse;
I have the extra lazy collection option enabled because I frequently query the size() of this collection without ever using the elements, and thus want Hibernate to execute a COUNT query rather than load the collection. The @Where condition on this collection specifies that I only want duels where the challenger has played (and thus has a score) and the challengee has not played, and thus does not have a score.
However, when calling size() on my collection, the generated sql is simply:
Quote:
select count(duel_id) from duels where challenger_id =?
As you can see, the count statement generated because of the extra lazy collection ignores the @Where condition on the collection. The SQL generated if I actually load the collection is correct, but unfortunately calling size() on that collection returns the same as if the collection had not had the @Where annotation.
I have been unable to find any documentation indicating that this is the desired behavior, as well as any documentation addressing what should happen in this case. As such, I think it is implied that the @Where condition should apply to all queries generated for the collection, including COUNTs done by extra lazy loading.
Has anybody encountered this before? Does anybody know if this is the intended behavior? If so, can anybody explain why this is the intended behavior?