I recently encounter this kind of problematic requirement: making a native SQL in hibernate dynamic. The query should be written as a template. And based on the user selection, a concrete SQl statement is derived.
It’s pointless to have this conversation if we use HQL’s query criteria of course. But with native SQL code, I admit there is no easy/legitimate way around but hack.
Technology stack: Hibernate with JPA through Srping JpaTemplate helper.
So for example, suppose this is what my SQL template looks like
Hibernate Native SQL
This is what the coressponding Java code looks like