-->
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' not working in Hibernate 3.1
PostPosted: Thu Jan 19, 2006 4:45 am 
Newbie

Joined: Tue Dec 27, 2005 3:30 am
Posts: 11
How does the "default" column attribute work in Hibernate 3.1? I have set the default column value and hbm2dll shows the column default, but when I persist the object the default value is not saved.
Here is the mapping document:

<class entity-name="TestPerson" table="TEST_PERSON"
name="com.sitepen.jsonpersistence.test.TestPerson">
<id name="Id" column="testPersonId">
<generator class="native"/>
</id>
<dynamic-component name="Members">
<property name="firstName" type="string"/>
<property name="lastName" type="string"/>
<property name="testProperty" type="string">
<column name="testProperty" default="'default string'" not-null="true"/>
</property>
</dynamic-component>
</class>

Here is the output from hbm2dll (for HSQLDialect):

create table TEST_PERSON (
testPersonId bigint generated by default as identity (start with 1),
firstName varchar(255),
lastName varchar(255),
testProperty varchar(255) default 'default string' not null,
primary key (testPersonId)
);

But this blows up with a null value in the not-null column instead of inserting the default. Why is that?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 20, 2006 6:17 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
You forgot the part:
Code:
<property name="testProperty" type="string" insert="false">
    <column name="testProperty" default="'default string'" not-null="true"/>
</property>

By SQL standard a column having a default value defined will have that default value take effect as long as the column is not referenced in the insert statement...


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 23, 2006 4:36 pm 
Newbie

Joined: Tue Dec 27, 2005 3:30 am
Posts: 11
Thanks - Not sure I understand. The defintion of the "insert" attribute sounds like it is for derived columns that you NEVER want to insert. The usual SQL use of DEFAULT is allow inserts to that column and only use the default if the column value is not set in the insert. I'll test it.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 23, 2006 5:44 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Then you want the dynamic-insert attribute of the <class/> mapping (which has performance implications). See http://www.hibernate.org/hib_docs/v3/reference/en/html/mapping.html#mapping-declaration-class


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 31, 2006 7:14 am 
Regular
Regular

Joined: Tue Nov 29, 2005 12:31 pm
Posts: 75
Hi,

My question is: why this is not working on hibernate 3.0.5 ?

I was using:

<class name="Name" table="Table" dynamic-insert="true">

<property name="defaultProperty" type="java.lang.String">
<column name="defaultProperty" default="'default string'"/>
</property>

Thanks in advance.


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.