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: Setting default values for columns in mapping files.
PostPosted: Wed Sep 12, 2007 2:54 pm 
Newbie

Joined: Wed Sep 12, 2007 1:58 pm
Posts: 12
Hi folks,

I'm trying to leverage the default column values in my database through Hibernate, but I can't determine the right XML to use in my mapping file. Here's the situation.

The column bar in the table foo is defined as:

Code:
bar Enum('tic','tac','toe') NOT NULL DEFAULT 'tic'


As you can see, the column does not allow nulls and has a default value.

I have an object foo, with getBar() and setBar(java.lang.String) methods. If I try to persist the object foo when foo.getBar() returns null, I want Hibernate to not send an explicit null for column bar to the database. This would allow the database to set column bar to value 'tic'.

Is it possible to configure this behavior in the Hibernate mapping file for table foo? I have this property for column bar:

Code:
<property name="bar" column="bar" type="java.lang.String" not-null="false" />


I checked the documentation and saw the insert=false attribute for the property tag, but when I tried it foo.setBar('toe') has no effect when foo is persisted, meaning the column will always have value 'tic'.

Alternatively, a less desirable solution is for the Hibernate to explicitly send the default value 'tic' when foo.getBar() returns null. I tried using the
Code:
<column name="bar" ... default="tic" />
tag to do this, but to no avail.

Can someone please shed some light on this issue? It would be very helpful to see a working XML snippet.

Thanks very much in advance,
mers


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 14, 2007 11:38 am 
Newbie

Joined: Tue Sep 04, 2007 2:21 pm
Posts: 14
Basically you need to tell Hibernate to not include the column when it is null. To do this - on the class tag specify dynamic-update="true" and dynamic-insert="true". That will allow the insert or update to not include any column that is null. By default this is false so Hibernate can pre-generate the insert and update SQL needed for the class.

Also look at the generated attribute on the property tag. You way want to use this on your default value columns as well.

HTH
Tom


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 18, 2007 6:00 am 
Newbie

Joined: Wed Sep 12, 2007 1:58 pm
Posts: 12
Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 18, 2007 8:52 am 
Newbie

Joined: Wed Sep 12, 2007 1:58 pm
Posts: 12
I took a look at the dynamic-insert attribute for class and the generated attribute for property.

When I tested these out, I get this behaviour:

1. If I don't mark a property as generated, Hibernate doesn't issue a followup SELECT, and thus doesn't refresh the object with the default column value generated by the database.

2. If I do mark a property as generated, Hibernate defaults the property to non-insertable and non-updateable mode. This means Hibernate never sends the property to the database, so calling the object's setter for that property will have no effect when the object is persisted. Without a working setter, the default can never be overridden.

The latter point is documented here.

Not exactly desirable behaviour, eh?

Hibernate support for default columns is broken in my opinion. There is no obvious reason why generate forces non-insertable mode.


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.