-->
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: Duplicate objects in Criteria.list() ?
PostPosted: Fri Jan 12, 2007 10:14 am 
Regular
Regular

Joined: Thu Oct 19, 2006 12:07 pm
Posts: 75
hibernate ver : 3.2.0.cr2 (part of JBoss 4.0.4GA)

Is it normal for a criteria to return duplicate objects?

Like :
Code:
Criteria crit = getSession().createCriteria(Group.class);

crit.add(some criterions);

crit.addOrder(Order.asc("comp_id.key"));

List result = crit.list();

if (result.get(1) == result.get(2)){
            System.out.println("woot ?");
        }


I tried to add crit.setResultTransformer(DISTINCT_ROOT_ENTITY), which appears to work, but the underlying SQL still returns more rows.
Is it worth optimising the SQL (by tweaking the Criteria, not touching the SQL directly, of course).


Details :

I have a Group class which has a Set of Member classes.

What I want is to get a list of Groups, that match certain conditions (err... Criterions) AND have a non-zero number of Members, that also match some conditions.

What I do now is this :

Code:
Criteria crit = getSession().createCriteria(Group.class);

crit.add(eq("type",3));

crit.createAlias("members", "member").add(gt("member.termination",
                        new Date()));

crit.addOrder(Order.asc("comp_id.key"));

List result = crit.list();

if (result.get(1) == result.get(2)){
            System.out.println("woot ?");
        }


I also tried createCriteria("members") instead of createAlias("members", "member") but the result is the same : duplicates.

What happens is, that if a certain Group matches the "type" and has for example 5 members out of which 3 match the "termination", then this group will be 3 times in the criteria result list.

What is the cleanest way to get rid of the duplicates ?

Finally I just need a list of Groups, not info about how many active members they have.


Here is the relevant part of the mapping:
Code:
<set name="members" lazy="true" inverse="true" cascade="none">
         <key>
            <column name="GROUP_KEY" />
            <column name="GROUP_KEY_PART2" />
         </key>
         <one-to-many
            class="com.foo.bar.Member" />
      </set>


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 16, 2007 3:46 am 
Regular
Regular

Joined: Thu Oct 19, 2006 12:07 pm
Posts: 75
It turns out to be a FAQ :
"Query result lists are always exactly the same size as the underlying JDBC ResultSet"


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:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.