Note that there are some explanatory texts on larger screens.

plurals
  1. POHibernate Criteria Query ...Adding a new condition
    primarykey
    data
    text
    <p>I have a criteria query that works just fine.</p> <pre><code>Criteria criteria = getSession().createCriteria(getPersistentClass()); criteria.add(Restrictions.eq("employerId",employerId)) .setFetchMode("card", FetchMode.JOIN) .createCriteria("card") .addOrder(Order.desc("cardId")) .createCriteria("salary") .add(Restrictions.eq("salaryType",SalaryIdentifierType.CONTRACTOR)) .add(Restrictions.eq("active","YES")); </code></pre> <p>Now, i need to add another filter(condition) to this criteria. The new filter is <code>joinDate</code>. If the <code>joinDate</code> value has been passed from the Frontend, i will have to add it to this query or else, the filter <code>joinDate</code> should not be added.</p> <p>I have managed to do this using a disjunction(I know, its weird...but AFAIK, only disjunction gives the the facility to add the filter at the runtime, only it if is present). I did it like this, but i do not want to use a disjunction here and I am looking for other options.</p> <pre><code>Criteria criteria = getSession().createCriteria(getPersistentClass()); ------------------------------------------------------------------------- Disjunction disjunctionDate = Restrictions.disjunction(); if(utilDate!=null){ disjunctionDate = (Disjunction) disjunctionDate.add(Restrictions.ge("startDate", utilDate)); } ----------------------------------------------------------------------- criteria.add(Restrictions.eq("employerId",employerId)) .setFetchMode("card", FetchMode.JOIN) .createCriteria("card") .addOrder(Order.desc("cardId")) .createCriteria("salary") .add(Restrictions.eq("salaryType",SalaryIdentifierType.CONTRACTOR)) .add(Restrictions.eq("active","YES")) .add(disjunctionDate); </code></pre> <p>I have tried just using if condition, like this but does not work(To even make this compile, i had to modify the query format from above to having criteria in every line and semicolon in every line).</p> <pre><code>Criteria criteria = getSession().createCriteria(getPersistentClass()); criteria.add(Restrictions.eq("employerId",employerId)); criteria.setFetchMode("card", FetchMode.JOIN); criteria.createCriteria("card"); criteria.addOrder(Order.desc("cardId")); criteria.createCriteria("salary"); criteria.add(Restrictions.eq("salaryType",SalaryIdentifierType.CONTRACTOR)); criteria.add(Restrictions.eq("active","YES")); if(startDate!=null){ criteria.add(Restrictions.ge("startDate",startDate)); } </code></pre> <p>Any ideas on how to make this work with better options than using disjunction?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload