-->
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.  [ 4 posts ] 
Author Message
 Post subject: Specifying default value for a property
PostPosted: Tue Dec 14, 2004 12:37 pm 
Regular
Regular

Joined: Thu Jan 29, 2004 10:34 am
Posts: 52
Location: Austin, TX
i can set a property to not-null="true" which faithfully generates

Code:
objectState INTEGER not null


has anyone tried to set the default value so the generated ddl would look like

Code:
objectState INTEGER not null default 0


(i know this can be enforced while persisting the object but i need this for pre-existing data and data migration)

thanks
ravi

ps. i'm using xdoclet


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 14, 2004 1:24 pm 
Regular
Regular

Joined: Thu Oct 07, 2004 4:45 pm
Posts: 92
You might want to consider doing this in the JavaBean instead. Specifically, initialize the property member with a default value when it is declared and/or set a reasonable default value in the setter method whenever "null" is passed in.

By enforcing this at the bean level, your code is more portable. The behavior will be the same even if you switch from Hibernate to something else in the future.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 14, 2004 1:30 pm 
Newbie

Joined: Mon Aug 09, 2004 8:27 pm
Posts: 15
Location: Brazil
or even in a constructor...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 15, 2004 2:24 am 
Regular
Regular

Joined: Thu Jan 29, 2004 10:34 am
Posts: 52
Location: Austin, TX
the java side solution would probably work in most cases but for maintenance purposes you would want the mapping to be exactly same as what's in the database. here's a scenario:

i create a persistent object let's say with one field
Code:
public String getfield1()


over time i would have a table with data populate for field1.

if i change it now to have a new field, here's the migration issue

Code:
@hibernate.property not-null="true"
public int getIntField()


the update will issue an "alter table" to add the new column intField.

this will cause all the existing records to have the value NULL. the next time you load one of these record it's boom time (unless of course we pre populate the atribute)

i came up with the following solution.

i changed SchemaUpdate and Table classes to generate the DEFAULT keyword. i yet have to integrate with XDoclet so that you could possibly do something like this:

Code:
@hibernate.property not-null="true" default="0"


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