-->
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: How to find duplicates using Hibernate/JPA?
PostPosted: Wed Nov 18, 2009 9:48 pm 
Newbie

Joined: Thu Nov 27, 2008 11:05 pm
Posts: 9
I am having trouble figuring out how to identify duplicates in a table (where lastName and firstName are equal). I will give the working SQL and my attempted JPA.

Working SQL:
Code:
SELECT DISTINCT c1.*
FROM Contact c1   
   inner join Contact c2 on       
   c1.firstName = c2.firstName       
   AND c1.lastName = c2.lastName       
   AND c1.contactId <> c2.contactId
ORDER BY c1.lastName, c1.firstName


Attempted JPA:
Code:
      
String query = "select distinct c1 from Contact c1 "
   + " inner join Contact c2 "
   + " where c1.firstName = c2.firstName "
   + " and c1.lastName = c2.lastName "
   + " and c1.contactId <> c2.contactId "
   + " order by c1.lastName, c1.firstName";


I keep getting the following errors when I run the JPA code. Some of the name fields are null, and I don't know how to get "c2" part of the "path" since it is the same table. How does one solve this?

JPS/Hibernate Error Log:
ERROR: Path expected for join!
ERROR: Invalid path: 'c2.firstName'
ERROR: right-hand operand of a binary operator was null
ERROR: <AST>: unexpected end of subtree
ERROR: Invalid path: 'c2.contactId'
ERROR: right-hand operand of a binary operator was null


Top
 Profile  
 
 Post subject: Re: How to find duplicates using Hibernate/JPA?
PostPosted: Wed Nov 18, 2009 11:47 pm 
Newbie

Joined: Thu Nov 27, 2008 11:05 pm
Posts: 9
I think I figured it out. No "join" needed.

Code:
String query = "select distinct c1 from Contact c1, Contact c2 "
   + " where c1.firstName = c2.firstName "
   + " and c1.lastName = c2.lastName "
   + " and c1.contactId <> c2.contactId "
   + " order by c1.lastName, c1.firstName";


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.