-->
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.  [ 4 posts ] 
Author Message
 Post subject: criteria and collections
PostPosted: Sun Mar 23, 2008 6:27 pm 
Beginner
Beginner

Joined: Mon Jan 07, 2008 4:13 am
Posts: 22
Hi,

i have a problem with define criteria for collection (LIST).

my objectA:

Code:
@OneToMany(mappedBy = "...", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "owner_id")
private List<Load> loads;


object Load:

Code:
@ManyToOne (fetch = FetchType.LAZY)
@JoinColumn(name = "owner_id", referencedColumnName = "id", nullable = false)
private ObjectA objectA;


i need define criteria:

Code:
Criteria dateValidCriteria = session.createCriteria(ObjectA.class);

Disjunction disjunction = Restrictions.disjunction();

.... create disjunction .....

dateValidCriteria.createCriteria("loads").add(disjunction);


but, problem is I have too many rows in result:

the first object A with 3 load objects.
the second object A with 2 load objects.
the third object A with 1 load object.

the result:

first objectA - load1
first objectA - load2
first objectA - load3
first objectA - load1
first objectA - load2
first objectA - load3
first objectA - load1
first objectA - load2
first objectA - load3
second objectA - load1
second objectA - load2
second objectA - load1
second objectA - load2
third objektA - load1
third objektA - load2

why I have not this result:

first objectA - load1
first objectA - load2
first objectA - load3
second objectA - load1
second objectA - load2
third objektA - load1
third objektA - load2

thanks!

Ivan


Top
 Profile  
 
 Post subject: Adding Distinct clause
PostPosted: Mon Mar 24, 2008 2:23 am 
Newbie

Joined: Wed Mar 28, 2007 1:05 am
Posts: 8
Hi,

I think you can try using Distinct clause. In criteria API you filter the retrieved list using

import org.hibernate.transform.DistinctRootEntityResultTransformer;

return new DistinctRootEntityResultTransformer().transformList(list);

where list is the results u have got after executing Criteria. Important thing is doing distinct this way is totally different from having Distinct in select clause as SQL or HQL level, as here first you get the whole result set from DB and then you filter out the duplicates.

Another way of doing the same is

dateValidCriteria..setResultTransformer(new DistinctRootEntityResultTransformer());

before you do dateValidCriteria.list();

Regards
Deepak


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 24, 2008 6:35 am 
Beginner
Beginner

Joined: Mon Jan 07, 2008 4:13 am
Posts: 22
thank you for your answer.

my criteria:

Criteria dateValidCriteria = session.createCriteria(ObjectA.class).
setFetchMode("loads", FetchMode.JOIN).
setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);

but, result is still WRONG.

Ivan


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 24, 2008 10:43 am 
Beginner
Beginner

Joined: Mon Jan 07, 2008 4:13 am
Posts: 22
this is OK:

Criteria dateValidCriteria = session.createCriteria(ObjectA.class).
setFetchMode("loads", FetchMode.SELECT).
setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);

BUT, when i add condition for loads:

Criteria dateValidCriteria = session.createCriteria(ObjectA.class).
setFetchMode("loads", FetchMode.SELECT).
createCriteria("loads").
add(disjunction).
setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);

this fails.

Ivan


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.