2

I have a scenario where in i have to pass null to a SQL parameter through java but i am getting SQLGrammarException. If i am passing some value it works fine. Please guide if i am wrong somewhere.

Below is my code:

StringBuffer query;
    query = new StringBuffer(
    "SELECT * "+ 
         "FROM table(package.func(travel_type => travel_search_type("+
             "travel_place_no => :travelPlaceNo"+
             ")))" );
        Query query1 = em.createNativeQuery(query.toString());
    query1.setParameter("travelPlaceNo",null);

    searchresults = query1.getResultList();

Exception:

org.hibernate.exception.SQLGrammarException

This is what i do through SQL Developer and it works fine:

    SELECT *
 FROM table(package.func(travel_type => travel_search_type(
     travel_place_no => NULL))) ; 

Please guide.

12
  • 2
    there is a , after the :travelPlaceNo - is that a copy paste error or not? Commented Nov 25, 2016 at 11:46
  • i am sorry it was copuy paste while indenting.. corrected Commented Nov 25, 2016 at 11:49
  • 1
    Did you take a look at stackoverflow.com/questions/2123438/… ? Commented Nov 25, 2016 at 11:51
  • 1
    Did you missed one closing parenthesis? Commented Nov 25, 2016 at 11:59
  • 1
    Instead of using named parameter you can try using the positional parameter. Please give it a try. Commented Nov 25, 2016 at 12:30

1 Answer 1

3

While calling the 2 argument signatures of the method, null is not an allowed value. You can use, instead, the 3 argument signature setParameter(String name, Object val, Type type) specifying the data type and it should work.

EDIT:

Ok, I see there is also another problem: even if the replacemente worked, what you are trying to execute is a >= NULL. In this case, I'm afraid that you are going to have to handle mannually the StringBuffer creation. Maybe just force an always false condition without parameters if its null, like `1!=2', and otherwise just handle it as you are doing on your sql example (write mannually 'NULL' instead of the parameter placeholder).

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

10 Comments

What is it in the database? NUMBER? I'd just try Integer and give it a go
i am using Query query1. I didnt find those signatures.
@Rubasace If i am passing null it is throwing sqlgrammarexception bro.
Can you please give the complete classname of Query so I can check it?
javax.persistence.Query;
|

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.