-->
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.  [ 5 posts ] 
Author Message
 Post subject: Incorrect count of association size
PostPosted: Wed Jan 17, 2007 7:05 pm 
Newbie

Joined: Tue Dec 12, 2006 2:07 pm
Posts: 8
Hibernate version: 3.2.1 GA

It seems that Hibernate is incorrectly computing the size of an association in a particular case. I have an entity "List" that has a set of "Users" (subscribers). Users have an "information" field that is NULLABLE.

When I do myList.getSubscribers().size() it generates a correct query but counts the non NULL lines and returns an incorrect count.

Adding "nullable = true" to the @Column annotation did not help.

Does someone encountered the same problem before?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 17, 2007 11:23 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Are you saying that users with information = null are not subscribers and should not be in the list? You can add a where="information is not null" in your mapping to do that.

If the problem is that you want the collection to contain all associated users, but you want to count only the ones where information = null, then you'll have to do that manually. Loop through the collection and count the relevant ones yourself.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 22, 2007 5:45 pm 
Newbie

Joined: Tue Dec 12, 2006 2:07 pm
Posts: 8
All the users that join should be in the 'subscribers' collection, regardless of the information field being null or not.

The mapping look like

Code:
  @ManyToMany(fetch = FetchType.LAZY, cascade = {CascadeType.ALL})
  @JoinTable(
    name = "lists_subscribers",
    joinColumns = {@JoinColumn(name = "listid")},
    inverseJoinColumns = {@JoinColumn(name = "userid")})
  private Set<User> subscribers;


but when Hibernates queries the database and counts the number of rows, it select the last column and counts the non NULL entries. The last column happens to be a nullable one, thus yielding an incorrect count.

Thanks for your help! PL


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 22, 2007 7:27 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Just checked the code, and hibernate doesn't check that the column it uses for the getSize() query is not null. IMO that's a bug.

Your options are either to load the collection before calling size() (because the actual collection size is used then), or change the order that fields are mapped in. I haven't followed the hibernate code all the way, but it just selects column 0 from the mapping to use in the count clause: if you change what that column is, it should work. The only reason it wouldn't work is that hibernate sorts the columns when creating the mapping.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 22, 2007 7:40 pm 
Newbie

Joined: Tue Dec 12, 2006 2:07 pm
Posts: 8
Thanks! That's what I thought, but happy to see that a pro can verify.

PL


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

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.