I'm trying to execute an insert operation to an Oracle DB Table from Java with JPA.
The following is my code:
String coCode;
String coDescription;
/* some operations on data ... */
String queryStatement =
"insert into MANAGER_DATA (CO_CODE, CO_DESCRIPTION) values (?1, ?2)";
Query updateQuery = getEntityManager(context).createNativeQuery(queryStatement);
updateQuery.setParameter(1, coCode);
updateQuery.setParameter(2, coDescription.compareTo("") == 0 ? null : coDescription);
updateQuery.executeUpdate();
The problem is that coDescription
can be null in case of an empty String and, in that case, I want the null value to be added in the table as well (coCode
is primary key but coDescription
is nullable). Apparently I cannot pass a null
value to the setParameter
function directly, as I am doing above.
How can I solve this and allow the null value to be added to my DB table?