-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Problem and fix using enhanced table generator with DB2
PostPosted: Thu Jun 04, 2009 11:54 am 
Newbie

Joined: Mon Sep 08, 2008 5:19 pm
Posts: 5
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?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.