简体   繁体   中英

hql many to many relation where info is contained in the join table

I have two entities Race and Player, and hibernate created 1 table race_player as Race and Players are linked by a ManyToMany relation.

I need to get the list of the races with :

  • the status "available"
  • more than 1 player registered for each of these races.

I've been searching the whole day to get in hql format the following query :

select racename from race where status = 'available' and idrace in ( select raceid from race_player group by raceid having count(raceid) > 1);

The problem is that the table race_player cannot be used in a hql query.

I don't see how I can get this information as the number of player is only referenced in the race_player table.

Thank you for your help !

Yes, you are right. You can't use race-player table in hql as there is no class mapped to that table directly. You would have list of players in Race class. Try to use:

Select raceName from Race r where r.status = 'available' and r.playersList.size >1

Hope it helps.

Thank you for your help, I was very close to find the solution. In fact, it was a syntax problem. Raman, I tried your solution and it was almost correct. I had to write the request without spaces between the string comparison, like this :

Select raceName from Race r where r.status='available' and r.playersList.size >1

Creazy isn't it ? I've lost many hours and a few hairs but finally so happy to make this sh*t running without exceptions. Once again, thank you !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM