Using the new JPA 2.1 stored procedure call, is there any way to pass a null parameter?
Here is an example usage:
StoredProcedureQuery storedProcedur
I ran into this same issue. I didn't have control over the stored procedure, so I couldn't modify it to accept default values.
However, because the database I was using was an Oracle database, I was able to work around this issue by changing the datatype of my stored procedure parameter (in orm.xml
) to java.lang.String
and then substituting an empty String
(""
) where it would have been appropriate to use a NULL
value. Since Oracle treats empty String
s (""
) and NULL
s identically, I was able to effectively trick Hibernate into passing a "null" value to the Oracle stored procedure.
If you don't want to have to resort to writing low-level JDBC code, your stored procedure is not modifiable, and your database is Oracle-based, try this approach. Just be sure that all of your entity manager code treats the parameter as though it were the intended data type (java.math.BigDecimal
in my case), and wait until you're setting the parameter value to convert to java.lang.String
.
For example:
orm.xml:
Java:
public void callStoredProc(java.math.BigDecimal myNullableInParam) { EntityManager entityManager = EMF.createEntityManager(); StoredProcedureQuery query = entityManager.createNamedStoredProcedureQuery("storedProcName"); query.setParameter("my_nullable_in_param", (myNullableInParam == null ? "" : myNullableInParam.toString())); // set more parameters, execute query, commit transaction, etc. }