10

I'm using JPA, hibernate 3.

String sqlQuery = " FROM TraceEntityVO  where lotNumber =:lotNumber and mfrLocId=:mfrLocId and mfrDate=:mfrDate and qtyInitial=:qtyInitial and expDate=:expDate";
Query query = entityManager.createQuery(sqlQuery)
                .setParameter("lotNumber", traceEntityVO.getLotNumber())
                .setParameter("mfrLocId", traceEntityVO.getMfrLocId())
                .setParameter("mfrDate", traceEntityVO.getMfrDate())
                .setParameter("qtyInitial", traceEntityVO.getQtyInitial())
                .setParameter("expDate", traceEntityVO.getExpDate());

This query works like a charm when the there were no empty or null values. But there could be possible of null or empty value for traceEntityVO.getLotNumber(),traceEntityVO.getMfrLocId(),traceEntityVO.getExpDate().

In this case the value 'null' or '' is checked against the variable instead of is null condition. How do I handle when I'm not sure about the parameter value, either null or empty?

I don't want to construct the query dynamically based on the values if empty or null.

Is this possible?

Thanks in advance..

2 Answers 2

3

I think you really can't do that without a dynamic query.

Building such a query however is easy with the criteria API (hibernate) (JPA), did you consider it?

Sign up to request clarification or add additional context in comments.

1 Comment

I fixed with dynamic query. I constructed query based on the conditions and passed the constructed query to entity manager. It works.. Thanks.
2

I hope following code will sort your problem. Assuming getMfrDate and getExpDate will return Date Object and others either Number or String objects. But you can modify IsEmpty according to return types.

String sqlQuery = " FROM TraceEntityVO  where lotNumber :lotNumber 
and mfrLocId :mfrLocId and mfrDate :mfrDate and qtyInitial :qtyInitial and 
expDate :expDate";

Query query = entityManager.createQuery(sqlQuery)
            .setParameter("lotNumber", isEmpty(traceEntityVO.getLotNumber()))
            .setParameter("mfrLocId", isEmpty(traceEntityVO.getMfrLocId()))
            .setParameter("mfrDate", isEmpty(traceEntityVO.getMfrDate()))
            .setParameter("qtyInitial", isEmpty(traceEntityVO.getQtyInitial()))
            .setParameter("expDate", isEmpty(traceEntityVO.getExpDate()));


private String isEmpty(Object obj) {

    if(obj!=null) {

        if (obj instanceof java.util.Date) {
            return " = to_date('"+obj.toString()+"') ";
        } else if(obj instanceof String) { 
            return " = '"+obj.toString()+"' ";
        } else if (obj instanceof Integer) {
            return " = "+obj.toString()+" ";
          }
    } 

    return new String(" is null ");
 }  

1 Comment

do you risk sql injection attack here as it's sort of like adding a string in if object is a String?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.