-->
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.  [ 2 posts ] 
Author Message
 Post subject: Alter table with not null column behaviour
PostPosted: Tue Jul 15, 2014 12:52 pm 
Newbie

Joined: Tue Jul 15, 2014 3:02 am
Posts: 2
Hi,

There have been a modification in hibernate behaviour when adding column.
In JbossAS5 (hibernate3), when using the JPA @Column(nullable=false) with hibernate.hbm2ddl.auto=update, hibernate was :
- Creating a not null column if table does not exist
- Creating a nullable column if table already exists
This was quite useful for me, as I could use the same code on new databases and when migrating existing databases (I was juste altering in SQL the column created by hibernate to add the not null constraint after updating the column with default values)

Since JBossAS7 and wildfly8 (hibernate4), the behaviour has been changed. When using the not null annotation, hibernate always tries to add a not null column and fails because there is already data in the table...

I cannot set a default value by annotation, because my code must work on different databases with different column type (and there is no standard annotation for that)...
My only solution seems to disable the hibernate.hbm2ddl.auto=update and alter the tables manually. The hibernate automatic alter table was a really cool feature...
Is there any way to configure hibernate to switch back to the previous behaviour (always adding nullable columns when altering a table) ?

Thanks for your help


Top
 Profile  
 
 Post subject: Re: Alter table with not null column behaviour
PostPosted: Wed Aug 27, 2014 10:41 am 
Newbie

Joined: Tue Jul 15, 2014 3:02 am
Posts: 2
I did not get any answers, so I looked into the code.

Here is the modification between Hibernate3 and Hibernate4 in the Table class :
In Hibernate 3.1.3 :
if (defaultValue!=null)
{
alter.append( " default ").append(defaultValue);
if ( column.isNullable() ) {
alter.append( dialect.getNullColumnString() );
}
else {
alter.append( " not null" );
}
}
The not null was set only if there was a default value.

In hibernate 4.1.4 (and latest) :
if ( defaultValue != null ) {
alter.append( " default " ).append( defaultValue );
}
if ( column.isNullable() ) {
alter.append( dialect.getNullColumnString() );
}
else {
alter.append( " not null" );
}

The not null is always set when altering a table.
Which cannot work if there is no default value and data in the table!

Why has this code been modified ?
This does not seem logical...


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

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.