-->
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.  [ 1 post ] 
Author Message
 Post subject: Hibernate criteria for distinct on multiple columns
PostPosted: Tue Jun 25, 2013 1:55 am 
Newbie

Joined: Tue Jun 18, 2013 1:36 pm
Posts: 4
I have to write criteria query for the following sql query

SELECT distinct ON(applicant.name,applicant_login.applicant_login_id)
applicant_login.applicant_login_id,
applicant.name,
applicant_address.address1 ,
applicant_address.address2 ,
applicant_address.postoffice ,
applicant_address.district,
applicant_address.state ,
applicant_address.pincode,
applicant.pan_no ,
applicant.nationality
FROM
public.applicant INNER JOIN public.applicant_login ON (applicant_login.applicant_login_id=applicant.applicant_login_id)
INNER JOIN public.application ON ( applicant.app_service_id = application.app_service_id)
INNER JOIN public.stock_register ON (application.application_id = stock_register.application_id )
LEFT JOIN public.applicant_address ON ( applicant.app_service_id = applicant_address.app_service_id )
LEFT JOIN public.address_type ON (applicant_address.address_type = address_type.address_type_id)
WHERE address_type.address_type_id = 'T'
ORDER BY applicant.name,applicant_login.applicant_login_id,applicant.app_service_id desc

So I have written as follows

Criteria crt=getSession().createCriteria(Applicant.class,"ant");
crt.createAlias("ant.applicantLogin", "al");
crt.createAlias("ant.applications", "ap");
crt.createAlias("ap.stockRegisters", "stock");
crt.createAlias("ant.applicantAddresses","ads",CriteriaSpecification.LEFT_JOIN);
crt.createAlias("ads.addressType","adst",CriteriaSpecification.LEFT_JOIN);

ProjectionList proj=Projections.projectionList();
proj.add(Projections.distinct(Projections.property("ant.name")));
proj.add(Projections.distinct(Projections.property("al.applicantLoginId")));
proj.add(Projections.property("ads.address1")) ;
proj.add(Projections.property("ads.address2")) ;
proj.add(Projections.property("ads.postoffice"));
proj.add(Projections.property("ads.district"));
proj.add(Projections.property("ads.state"));
proj.add(Projections.property("ads.pincode"));
proj.add(Projections.property("ant.panNo")) ;
proj.add(Projections.property("ant.nationality")) ;
crt.addOrder(Order.asc("ant.name"));
crt.addOrder(Order.asc("al.applicantLoginId"));
crt.addOrder(Order.asc("ant.appServiceId"));
crt.setProjection(proj);
crt.add(Restrictions.eq("adst.addressTypeId","T"));

But execution of these query throws exception

Caused by: org.postgresql.util.PSQLException: ERROR: syntax error at or near "distinct"

I can't understand what is wrong with this query.Please advice me.Thanks in advance


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

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.