-->
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.  [ 2 posts ] 
Author Message
 Post subject: Why does JOIN FETCH return duplicates?
PostPosted: Wed Aug 24, 2016 9:06 am 
Newbie

Joined: Wed Aug 24, 2016 8:52 am
Posts: 1
Hi,

I'm using hibernate-5.2.1-JPA2.1 via openejb-core-7.0.1 in Java SE 8 with Postgresql 9.5.4 env.

I have a 1 (BuddyRelation) :n (BuddyPermission) Relation

The query

Code:
SELECT br FROM BuddyRelation br LEFT JOIN FETCH br.buddyPermissions WHERE br.account.accountId = :accountId


returns not only the distinct BuddyRelations but a row for each 1:n BuddyPermission.
I would have expected that only the distinct BuddyRelation entries are returned, with properly filled buddyPermissions Map.

Code:
class BuddyRelation
   ...
   @OneToMany(mappedBy = "buddyRelation", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
   @OrderBy("permission ASC")
   @MapKey(name = "permission")
   @OnDelete(action = OnDeleteAction.CASCADE)
   public Map<String, BuddyPermission> getBuddyPermissions() {
      return buddyPermissions;
   }
  ...
}


What is wrong?

Thanks
Marcel


Top
 Profile  
 
 Post subject: Re: JOIN FETCH returns too many rows?
PostPosted: Mon Aug 29, 2016 5:54 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
Because the underlying JDBC ResultSet contains one BuddyRelation entity for each BuddyPermission being fetched, so does the List<BuddyRelation> will contain duplicates. You need to use DISTINCT to remove those duplicates from the entity List:

Code:
SELECT DISTINCT br FROM BuddyRelation br LEFT JOIN FETCH br.buddyPermissions WHERE br.account.accountId = :accountId


You can upgrade to 5.2.2, and make use of the [url=new http://in.relation.to/2016/08/04/introd ... uery-hint/]DISTINCT pass-through Hibernate Query Hint[/url].


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.