简体   繁体   中英

Similar SQL queries in different methods

There are two similar methods:

Collection<Movie> findAll(UserDetails userDetails); //find all movies rated by User
Collection<Movie> findAll(UserDetails userDetails, String s); //find all movies rated by User and containing "s" in title

So, the second is first + regex. JPQL code respectively:

select movie........ //doesn't mater
//the same logic

and

select movie........ where lower(movie.title) like :s
//the same logic

All the difference is one line of code, but it forces me to code repeating in these two methods. How can I avoid code repeating in this case?

I thought to implement private method that will receive the queries from these two and process "the same logic". Is it good approach?

Keep the both methods and just create another method which receives your result and process the data.

Collection<Movie> findAll(...) { // your params here
Result result = select movie........ //doesn't mater
return processData(result);
}

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