-->
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: how to find records that column "c2" repeated 2 times by hql
PostPosted: Sun Nov 13, 2016 9:01 pm 
Newbie

Joined: Sun Nov 13, 2016 8:40 pm
Posts: 3
there is a table named "tableA",
and the column(first row) and records as below:
"id" "c2" "c3"
1 zhan1 wu
2 zhan1 go
3 chan1 th
4 zhuang cn

now,you can see "zhan1" repeated 2 times in column "c2"
so I want find the first record and second record by HQL.
and I know how to use sql resolve this problem:
Code:
select a from tableA a inner join
(select b.c2 from tableA b group by b.c2 having count(b.c2)=2 ) c on a.c2=c.c2

some one knows how to use HQL resolve this probelm???


Top
 Profile  
 
 Post subject: Re: how to find records that column "c2" repeated 2 times by hql
PostPosted: Mon Nov 14, 2016 1:33 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1638
Location: Romania
You can use this HQL query:

Code:
select a1
from EntityA a1
where a1.id in (
    select a2.id
    from EntityA a2
    group by a2.c2
    having count(a2.c2) = 2
)

_________________
If you liked my answer, you are going to love my High-Performance Java Persistence book and my blog as well.


Top
 Profile  
 
 Post subject: Re: how to find records that column "c2" repeated 2 times by hql
PostPosted: Mon Nov 14, 2016 2:41 am 
Newbie

Joined: Sun Nov 13, 2016 8:40 pm
Posts: 3
thank you for your reply.
I have try your way lastday ,but it is so slow .beacuse I have a lot of data.
any other way?


Top
 Profile  
 
 Post subject: Re: how to find records that column "c2" repeated 2 times by hql
PostPosted: Mon Nov 14, 2016 5:58 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1638
Location: Romania
Sure. Try adding an index on tableA.c2 column.

_________________
If you liked my answer, you are going to love my High-Performance Java Persistence book and my blog as well.


Top
 Profile  
 
 Post subject: Re: how to find records that column "c2" repeated 2 times by hql
PostPosted: Mon Nov 14, 2016 8:00 pm 
Newbie

Joined: Sun Nov 13, 2016 8:40 pm
Posts: 3
yes,it is a good idear,I'll try it ,thank you.


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.