I am using hibernate. I need to create query below. It is possible the subContest is null in which case subContest_id=:cont should NOT be part of where clause. In conventional SQL when creating dynamic sql ; for such cases I used to do subContest_id = subContest_id ; BUT how do I achieve it in case below. Here If I try to query.setParameter( "cont", "subContest_id " ); it will give exception as cont is supposed to be an id and not a string.
Query query = getEntityManager().createQuery( "select * from Upload where moderated=:mod, subContest_id=:cont order by lastModifiedTime desc " );
query.setParameter( "cont", subContest.getId() );
you can try using criteria query in hibernate
you can add your where clause as restrictions
in your case you want to check null before adding restriction
and finally order by can be done using this
please refer here for details on how to use criteria queries