DB2 SQL Error: SQLCODE=-302, SQLSTATE=22001, SQLERRMC=null

By | February 10, 2016

This error is the DB2 cryptic way of informing you that a value which an insert query tries to insert in a table is too large.

Usually when using java + hibernate we can define a member of an EJB as:


@Column(length = 10)
protected String receiver;

If somehow when the object is created we are allowed to set:

receiver="MyLongReceiverName";

when hibernate will try to persist the object into DB2 we will get:

DB2 SQL Error: SQLCODE=-302, SQLSTATE=22001, SQLERRMC=null

To avoid this issue make sure when defining columns with a restricted size to perform a size check before committing the object to the db.

In case an update to the column length is necessary, note that hibernate is not able to update the size of the column associated with the “receiver” member after changing the definition to:

@Column(length = 20)
protected String receiver;

The same issue will occur after a restart, hibernate will ignore the size change.

To properly update the table and avoid the error execute:

ALTER TABLE My_Table ALTER COLUMN receiver SET DATA TYPE VARCHAR(20);

Advertisements