Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
281 views
in Technique[技术] by (71.8m points)

java - Write Spring Specification to filter unrelated tables

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 .

question from:https://stackoverflow.com/questions/65910226/write-spring-specification-to-filter-unrelated-tables

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...