-->
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: joined criteria query returns same instance multiple times?
PostPosted: Mon Mar 07, 2011 5:08 pm 
Newbie

Joined: Mon Mar 07, 2011 4:43 pm
Posts: 3
Hello,
I am working in a Java framework where I want to restrict an hibernate query, where I have to do a simple join. But hibernate seems to return the same result multiple times:

a “Deal” refers, through its member “creator” to a “User”. A “User” has a field “username”. My restrictive code is:

criteria.createAlias("creator","cr").add(Restrictions.eq("cr.userName", session.getPrincipal().getName()))

It works only I get the same deal instance as many times as there are users. I tried
setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
But it doesn’t help.

Since I am writing this restriction in a generic way, for classes that I do not know on advance, I look for the most simple solution.

Thanks for your insights,
Marc


Top
 Profile  
 
 Post subject: Re: joined criteria query returns same instance multiple times?
PostPosted: Wed Mar 09, 2011 1:59 pm 
Regular
Regular

Joined: Fri Jan 28, 2011 11:44 am
Posts: 117
Hi,

Can you post the whole criteria call and the generated SQL query?


Top
 Profile  
 
 Post subject: Re: joined criteria query returns same instance multiple times?
PostPosted: Wed Mar 09, 2011 3:31 pm 
Newbie

Joined: Mon Mar 07, 2011 4:43 pm
Posts: 3
Hello,

thanks for your reply.

The SQL generated by hibernate is the following:

select this_.ID as ID0_2_, this_1_.VERSION as VERSION0_2_, this_1_.CREATOR_ID as
CREATOR3_0_2_, this_1_.S_ASSIGNEE as S4_0_2_, this_1_.S_OBJECT_TYPE as S5_0_2_,
this_.S_OBJECT_TYPE as S2_1_2_, this_.NAME as NAME1_2_, this_.COMMENT as COMMEN
T1_2_, this_.AMOUNT as AMOUNT1_2_, this_.CREATOR_ID as CREATOR6_1_2_, cr1_.ID as
ID2_0_, cr1_.VERSION as VERSION2_0_, cr1_.S_OBJECT_TYPE as S3_2_0_, cr1_.CREATO
R_ID as CREATOR4_2_0_, cr1_.USER_NAME as USER5_2_0_, cr1_.ID as ID2_1_, cr1_.VER
SION as VERSION2_1_, cr1_.S_OBJECT_TYPE as S3_2_1_, cr1_.CREATOR_ID as CREATOR4_
2_1_, cr1_.USER_NAME as USER5_2_1_ from DEAL this_ inner join TRACEABLE this_1_
on this_.ID=this_1_.ID inner join USER cr1_ on this_1_.CREATOR_ID=cr1_.ID inner
join USER cr1_ on this_.CREATOR_ID=cr1_.ID where cr1_.USER_NAME=? order by this_
.ID desc


My Java code is in fact the implementation of a hook method in a Java framework. I give here the implementtaion of this method:

public void refineCriteria(EnhancedDetachedCriteria criteria, IQueryComponent queryComponent, Map<String, Object> context){


IApplicationSession session = getBackendController(context).getApplicationSession();
Boolean hasRole = SecurityHelper.isSubjectGranted(
session.getSubject(),
new roleHolder(role)
);

if (hasRole && (queryComponent.getQueryContract().getName().contains(businessObject))){
criteria.createAlias("creator","cr",CriteriaSpecification.LEFT_JOIN).add(Restrictions.eq("cr.userName", session.getPrincipal().getName())).setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);

}
}


Top
 Profile  
 
 Post subject: Re: joined criteria query returns same instance multiple times?
PostPosted: Wed Mar 09, 2011 3:56 pm 
Regular
Regular

Joined: Fri Jan 28, 2011 11:44 am
Posts: 117
Hi,

Can you post how the criteria property passed in argument is created?


Top
 Profile  
 
 Post subject: Re: joined criteria query returns same instance multiple times?
PostPosted: Fri Mar 11, 2011 1:43 pm 
Newbie

Joined: Mon Mar 07, 2011 4:43 pm
Posts: 3
Hi,

I could locate the problem. Error is in my code. After many trials I forgot to remove a double column.

Sorry for the false post and thanks for reaching out.

Marc


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.