Environment: Hibernate 3.3.1.GA using DB2Dialect, DB2 UDB 8.2
I wanted to use a custom optimizer with the enhanced table generator. My first test uncovered a problem with the code in org.hibernate.id.enhanced.TableGenerator. The configuration process creates this table by default:
Code:
create table hibernate_sequences (sequence_name varchar(255),
next_val bigint, primary key (sequence_name))
In most databases that would be fine. But DB2 requires NOT NULL on all key columns, instead of inferring it like others do. There does not seem to be a way to specify this in the configuration. I was able to get it working by extending the TableGenerator class:
Code:
package foo.db;
import org.hibernate.id.enhanced.TableGenerator;
import org.hibernate.dialect.Dialect;
import org.hibernate.HibernateException;
import java.sql.Types;
public class DB2TableGenerator extends TableGenerator {
public String[] sqlCreateStrings(Dialect dialect) throws HibernateException {
return new String[] {
new StringBuffer()
.append( dialect.getCreateTableString() )
.append( ' ' )
.append( getTableName() )
.append( " ( " )
.append( getSegmentColumnName() )
.append( ' ' )
.append( dialect.getTypeName( Types.VARCHAR, getSegmentValueLength(), 0, 0 ) )
.append( " NOT NULL, " ) // <-- this is the significant change
.append( getValueColumnName() )
.append( ' ' )
.append( dialect.getTypeName( Types.BIGINT ) )
.append( " ) " )
.toString()
};
}
}
I did not see anything in the DB2Dialect that would be useful here, but that doesn't mean there isn't, it mostly means I don't understand enough about how the dialect code works.
I'm not sure how best to fix this in the Hibernate code. Maybe it would be OK to always specify NOT NULL even if the database doesn't require it -- would that break anything?