0

I am trying to write a query which will check if all items in an array belong in a single record.

Just like for two variables I can write

 @Query( "from #{#entityName} c where c.channelType = 'group' and exists ( from UserChannel u where c.id = u.channel.id and u.user.id = :userId ) and exists (from UserChannel u where c.id = u.channel.id and u.user.id = :userId2)" )
    Set< Channel > findGroupChannelsByUserIds( @Param( "userId" ) int userId, @Param( "userId2" ) int userId2 );

If my second parameter is an array of userids, how can I write the query then ?

userx
  • 1,083
  • 5
  • 18
  • 36

2 Answers2

0

I'm not 100% what you mean by "all the elements of the array meet a condition". But looks like what you need is an 'in' condition.

You can say

...u.user.id in :userId2

where userId2 is a List. (works with Spring data 1.9.4 - may not work with much earlier versions)

BhathiyaW
  • 356
  • 3
  • 11
  • Thanks for your answer. What I meant is if I have a array of userid's which have to meet a condition like exists ( from UserChannel u where c.id = u.channel.id and u.user.id = :userId , then how can I write that query. – userx May 20 '16 at 16:45
0

I followed this answer : Spring JPA Repository dynamic query to solve it.

I had to create another interface and provide implementation of that. Then I extended that interface in my repository.

Community
  • 1
  • 1
userx
  • 1,083
  • 5
  • 18
  • 36