-->
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.  [ 5 posts ] 
Author Message
 Post subject: column default values
PostPosted: Wed Jun 08, 2005 8:20 pm 
Newbie

Joined: Wed Jun 08, 2005 7:49 pm
Posts: 3
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3.03

Mapping documents:

Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:

Name and version of the database you are using:ms sql server 2k

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:

I've tried for hours to find a way to specify default value of 0 for integer columns and can't. Does anyone know how? Also, what I really need to be able to do for production (and dec) usage of SchemaUpdate is add a column of type integer, not-null, default 0. But, the best I've been able to do is get schemaupdate to add an integer column as nullable without a default value.


Top
 Profile  
 
 Post subject: Re: column default values
PostPosted: Thu Jun 09, 2005 9:56 am 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
bataras wrote:

I've tried for hours to find a way to specify default value of 0 for integer columns and can't. Does anyone know how? Also, what I really need to be able to do for production (and dec) usage of SchemaUpdate is add a column of type integer, not-null, default 0. But, the best I've been able to do is get schemaupdate to add an integer column as nullable without a default value.


You can write your own PropertyAccessor implementation by creating a class that implements org.hibernate.property.PropertyAccessor and provides the behavior you wish to use.

Then, add access="xx.xx.ZeroDefaultIntegerPropertyAccessor" to your mapping files for fields that you wish to have this behavior.


Top
 Profile  
 
 Post subject: Re: column default values
PostPosted: Thu Jun 09, 2005 11:11 am 
Newbie

Joined: Wed Jun 08, 2005 7:49 pm
Posts: 3
pksiv wrote:
bataras wrote:

I've tried for hours to find a way to specify default value of 0 for integer columns and can't. Does anyone know how? Also, what I really need to be able to do for production (and dec) usage of SchemaUpdate is add a column of type integer, not-null, default 0. But, the best I've been able to do is get schemaupdate to add an integer column as nullable without a default value.


You can write your own PropertyAccessor implementation by creating a class that implements org.hibernate.property.PropertyAccessor and provides the behavior you wish to use.

Then, add access="xx.xx.ZeroDefaultIntegerPropertyAccessor" to your mapping files for fields that you wish to have this behavior.



Ok I'll look at that. But it seems really strange to me that a system as great and mature as hibernate doesn't have the notion of column default values. And also that the schemaupdater doesn't know how to add columns that way. BTW, will this propertyaccessor thing work when schemaupdate adds an int coumn of ZeroDefaultIntegerPropertyAccessor?

Also, I suppose I should offer it back to hibernate.org as a patch. I'll look for the instructions on doing that.

thanks


Top
 Profile  
 
 Post subject: Re: column default values
PostPosted: Thu Jun 09, 2005 1:41 pm 
Newbie

Joined: Wed Jun 08, 2005 7:49 pm
Posts: 3
bataras wrote:
pksiv wrote:
bataras wrote:

I've tried for hours to find a way to specify default value of 0 for integer columns and can't. Does anyone know how? Also, what I really need to be able to do for production (and dec) usage of SchemaUpdate is add a column of type integer, not-null, default 0. But, the best I've been able to do is get schemaupdate to add an integer column as nullable without a default value.


You can write your own PropertyAccessor implementation by creating a class that implements org.hibernate.property.PropertyAccessor and provides the behavior you wish to use.

Then, add access="xx.xx.ZeroDefaultIntegerPropertyAccessor" to your mapping files for fields that you wish to have this behavior.



Ok I'll look at that. But it seems really strange to me that a system as great and mature as hibernate doesn't have the notion of column default values. And also that the schemaupdater doesn't know how to add columns that way. BTW, will this propertyaccessor thing work when schemaupdate adds an int coumn of ZeroDefaultIntegerPropertyAccessor?

Also, I suppose I should offer it back to hibernate.org as a patch. I'll look for the instructions on doing that.

thanks



Actually, now that I've looked at the BasicPropertyAccessor implementation of PropertyAccessor, it doesn't appear to help with my problem. The problem is this: I want to be able to specify "default="n"" within a <column> or <property> object configuration (just like saying "not-null="true"") so that a) the generated schema from SchemaExport will create the table's column with "default n" just like it creates it with "NOT NULL" and b) the "alter table" script generated by SchemaUpdate will be able to add new columns as "NOT NULL" because they're set to have a "default n".

thanks


Top
 Profile  
 
 Post subject: Re: column default values
PostPosted: Thu Jun 09, 2005 2:52 pm 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
bataras wrote:

Actually, now that I've looked at the BasicPropertyAccessor implementation of PropertyAccessor, it doesn't appear to help with my problem. The problem is this: I want to be able to specify "default="n"" within a <column> or <property> object configuration (just like saying "not-null="true"") so that a) the generated schema from SchemaExport will create the table's column with "default n" just like it creates it with "NOT NULL" and b) the "alter table" script generated by SchemaUpdate will be able to add new columns as "NOT NULL" because they're set to have a "default n".

thanks


First off, I agree that it would be nice if there was a default="n" attribute available. But there isn't. You might want to search the JIRA to see if there is one opened for it. I find it hard to believe that nobody has requested it before but my guess is it was rejected.

That said... There are quite a few previous postings on this same topic in the past and looking back at some of them, one recommends setting the default values in your database DDL.

If this isn't an option, the PropertyAccessor will work. It's not as elegant as having a default="n" attribute, but it it doesn't require a hibernate schema and code change either.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.