Search code examples
javainsertupdatesjdbctemplate

java.sql.SQLException: ORA-01407: cannot update to NULL in Java


I have below code in Java where my Update statement is getting failed and i am getting an error java.sql.SQLException: ORA-01407: cannot update ("ORDER_ID_TEST"."SOURCES") to NULL. I really dont understand why the SOURCES field is not getting mapped. I have mentioned only some of the important part of the code. The orderid and other fields are getting mapped in update statement.

The SOURCES field is string of not null datatype in table. I am trying to check if combination of ORDERID and EXTORDID exist in table from file then do update or else insert.

Instead of if (!latestModDate.isEmpty()) if i give ((latestModDate != null)) the process run but its also inserting values and not checking for update statement.

@Service

public class OImporter { private final static Logger log = LoggerFactory.getLogger(OImporter.class);

// Tabellenname auf Spalten in der Tabelle
Map<String, List<ColumnData>> columnDataCache = new HashMap<>();

private final JdbcTemplate jdbcTemplate;

public OImporter(JdbcTemplate jdbcTemplate) {
    this.jdbcTemplate = jdbcTemplate;
}

public void importFile(BufferedReader bufferedReader, String tableName, String fileName,
                       Map<Integer, Function<String, String>> COLUMN_TYPE_MAPPINGS,
                       Map<String, Function<String, String>> COLUMN_NAME_MAPPINGS) throws IOException {
    try {
        importFile(bufferedReader, tableName, fileName, COLUMN_TYPE_MAPPINGS, COLUMN_NAME_MAPPINGS,
                new ArrayList<>());
    } catch (IllegalStateException e) {
        log.warn(e.getLocalizedMessage());
    }
}

Solution

  • In Oracle database, an empty string become a NULL value.

    Assuming bar is a VARCHAR2 column type, the following two statements are the same, i.e. they will set the value to NULL.

    UPDATE foo SET bar = ''
    
    UPDATE foo SET bar = NULL
    

    Your code fails because local variable sources is an empty string.

    Since you never declare or initialize columnName, it is unclear how that code even compiles, and we cannot tell you why sources remains an empty string.