2

I am working on a project Spring and Java, generated using JHipster. I want to filter on table that doesn't have a direct relationship with another.

My purpose is almost asked in a previous similar question
Write Spring Specification with multiple inner join & other conditions

But in my case , I ve two unrelated entities :
Consultant (id : Long , FullName : string , profileRank : Enum of string )
Rank (id : Long , level : Enum of string , rate : Double )

 Consultant                         |        Rank
                                    | 
id | FullName | profileRank         |        id | level    | rate      
1  | aaaaa    | 'ONE'               |        1  | 'ONE'    | 1
2  | bbbbbb   | 'THREE'             |        2  | 'TWO'    | 2
3  | cccccc   | 'FOUR'              |        3  | 'THREE'  | 3
4  | dddddd   | 'THREE'             |        4  | 'FOUR'   | 4

I want to filter consultant list by rate using level
Example : get consultants with rate greater than 3

Expected result  
id | FullName | profileRank      
3  | cccccc   | 'FOUR'    

I ve searched in documentation and many articles without get it to work please how to achieve that .

barsawi13
  • 153
  • 3
  • 12

1 Answers1

2

You don't need to write a specification for your case.

  1. Fetch all the ranks with levels and rates
  2. Filter these values and keep only the ones greater than 3 (step 1 and 2 can be combined). The result will be a List<Rank> that contains only FOUR rank
  3. list.stream(rank => rank.level).collect(toList())
  4. The result of step 3 will be passed to a repository method like List<Consultant> findByProfileRankIn(List<String> levelNames)

Another alternative would be joins something like this Joining tables without relation using JPA criteria

If you still want a spec that's also possible. Spring Data Join with Specifications

tzortzik
  • 4,993
  • 9
  • 57
  • 88