I am new to Derby, and I noticed that I am facing similar issues as when using DB2 RDBMS values ββwith respect to null values. The Derby documentation states that null must be of the type associated with it (that DB2 finally got rid of version 9.7):
http://db.apache.org/derby/docs/10.7/ref/crefsqlj21305.html
Now I'm trying to find a general solution to this problem here, as this will be part of my jOOQ database abstraction library. The following example just documents the problem. Think of another (more complex) example. The following does not work:
insert into T_AUTHOR ( ID, FIRST_NAME, LAST_NAME, DATE_OF_BIRTH, YEAR_OF_BIRTH, ADDRESS) select 1000, 'Lukas', 'Eder', '1981-07-10', null, null from SYSIBM.SYSDUMMY1
And it does not (which is what jOOQ actually does):
insert into T_AUTHOR ( ID, FIRST_NAME, LAST_NAME, DATE_OF_BIRTH, YEAR_OF_BIRTH, ADDRESS) select ?, ?, ?, ?, ?, ? from SYSIBM.SYSDUMMY1
Since the two null values ββdo not have the type associated with it. The solution would be to write something like this:
insert into T_AUTHOR ( ID, FIRST_NAME, LAST_NAME, DATE_OF_BIRTH, YEAR_OF_BIRTH, ADDRESS) select 1000, 'Lukas', 'Eder', '1981-07-10', cast(null as int), cast(null as varchar(500)) from SYSIBM.SYSDUMMY1
Or, accordingly,
insert into T_AUTHOR ( ID, FIRST_NAME, LAST_NAME, DATE_OF_BIRTH, YEAR_OF_BIRTH, ADDRESS) select ?, ?, ?, ?, cast(? as int), cast(? as varchar(500)) from SYSIBM.SYSDUMMY1
But very often in Java, the type that null should be dropped is unknown:
- In this example, types can be obtained from the insert clause, but this may be difficult or impossible for more general use cases.
- In other examples, I could choose any type for translation (for example, always casting to
int ), but this would not work in this example, since you cannot put the value of cast(null as int) in ADDRESS . - With HSQLDB (another candidate for this problem), I can simply write
cast(null as object) , which will work in most cases. But Derby is not of type object .
This problem still annoyed me DB2, and I have not yet found a solution. Does anyone know of a stable and general solution to this problem for any of these RDBMSs?