-->
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.  [ 10 posts ] 
Author Message
 Post subject: how can I set default value of one column in x.hbm.xml file?
PostPosted: Tue Sep 02, 2003 10:50 pm 
Newbie

Joined: Tue Sep 02, 2003 10:28 pm
Posts: 11
this is the main part of my xxx.hbm.xml file:

<hibernate-mapping>
<class name="com.animal.Clob" table="ANIMAL_CLOB">
<id name="id" column="ID" type="long" unsaved-value="null">
<generator class="hilo"/>
</id>
<property name="clob" column="CLOB" type="string" not-null="true"/>

<property name="createDate" column="CREATE_DATE" type="date" default="SYSDATE"/>
<property name="status" column="STATUS" type="integer" length="1" not-null="true"/>
</class>
</hibernate-mapping>

default="SYSDATE" is just where i need to set the default value of the column: CREATE_DATE.
i try it, BUT SAXParseException has been thrown while parsing this xml.
it seem that default can not be identified.

please tell me how can i do it?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 03, 2003 2:57 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
It looks like you are using Oracle. There is no way it would know that sysdate as a function for the default value. I suggest you manage this at the domain model level and set the field on construction of the domain object to todays date, eg, _createDate = new Date();

Another option is to place this into an interceptor which will get it closer to the actual time it hit the database (differnece would be minimal), or I supoose you could implement the net.sf.hibernate.Validatable interface on the domain object and check if it valid if not new Date().


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 03, 2003 5:48 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
There is no default attribute in Hibernate mappings.


If you want to do this use a trigger.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 03, 2003 8:41 pm 
Newbie

Joined: Tue Sep 02, 2003 10:28 pm
Posts: 11
thank you for you replay, and your endless patience!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 31, 2004 5:16 am 
Newbie

Joined: Thu Apr 01, 2004 12:06 pm
Posts: 12
Location: Hamburg (Germany)
@david: What about using the Lifecycle interfaces onSave method for default values?

@gavin: But when I have a not-null column i cannot use database triggers to fill the field, because hibernate complains that the field is empty!


Top
 Profile  
 
 Post subject: is this expected any time soon at all ?
PostPosted: Wed Feb 28, 2007 2:48 am 
Newbie

Joined: Wed Feb 28, 2007 2:43 am
Posts: 1
is this expected any time soon at all ?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 09, 2007 5:16 am 
Newbie

Joined: Mon Feb 19, 2007 5:10 am
Posts: 15
My workaround and issue...

The Task
I recently worked on an object which represents an uploaded file. We wanted this object to have an uploadDateTime field to store the date at which the record was created.

The Solution
I got this to work by setting up the database DDL thus:

Code:
updateDateTime DateTime NOT NULL DEFAULT GETDATE()


I updated the NHibernate mapping for that column:

Code:
<property column="uploadDateTime" type="DateTime" name="UploadDateTime" not-null="true" insert="false" update="false"


The insert="false" update="false" prevents NHibernate from using that field in INSERT and UPDATE statements, thereby forcing the database to generate the default value.

I wrote a unit test that saved down the object and then queried the database for that object and found that the uploadDateTime field had been populated. Success!

However...

The Problem
I noticed that upon saving a object, the property isn't automatically populated. This is because, although the record is created in the database and the field is populated, the field value is not then synchronised back up with the object!

The Workaround
I'm going to populate this field in my DAO layer before insertion.

The Feature Request
Couldn't NHibernate recognise that it would need to synchronise the field after insertion upon seeing the following combination of column attributes?
  • insert="false"
  • not-null="true"


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 20, 2007 9:20 am 
Newbie

Joined: Wed May 24, 2006 4:40 pm
Posts: 10
I had something similar setup... with the database setting default values.

The scheme breaks down when hibernate settings contain:
Code:
hibernate.hbm2ddl.auto=create


This recreates the database with the defaults missing.

Do you have an alternate way to get default values working?

Right now, I am trying to find a solution along this line:
Maybe there is a way to execute a set of sql statements right after hibernate recreates the database. I have the sql to set the default values back.

Any idea how I could achieve this ?

Thanks

Ajay

_________________
http://www.ajaygautam.com/


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 20, 2007 6:44 pm 
Newbie

Joined: Wed May 24, 2006 4:40 pm
Posts: 10
Ok. So I fixed it with the following (yet another) workaround.

In my case, hibernate.hbm2ddl.auto=create is set in junit so, in setUp(), I added a call to:

Code:
...
session.createSQLQuery("alter table MDI_REFDATA modify LME_IGNORE_CASH tinyint default 0 not null").executeUpdate();
session.createSQLQuery("alter table MDI_REFDATA modify MARKET_CENTER_TYPE tinyint default 0 not null").executeUpdate();
session.createSQLQuery("alter table MDI_REFDATA modify BASE_SERIES_ID tinyint default 0 not null").executeUpdate();
...


So, basically junit process fixes this now...

If anyone knows a better way, I am all ears...

Ajay

_________________
http://www.ajaygautam.com/


Top
 Profile  
 
 Post subject: Re: how can I set default value of one column in x.hbm.xml file?
PostPosted: Wed Sep 08, 2010 5:26 pm 
Newbie

Joined: Wed Aug 25, 2010 2:06 pm
Posts: 4
I updated the NHibernate mapping for that column:

Code:
<property column="uploadDateTime" type="DateTime" name="UploadDateTime" not-null="true" insert="false" update="false"


The insert="false" update="false" prevents NHibernate from using that field in INSERT and UPDATE statements, thereby forcing the database to generate the default value.


Has anyone tried insert="false" update="false" with an id element?
I get this error:
Multiple annotations found at this line:
- Attribute "update" must be declared for element type "id".
- Attribute "insert" must be declared for element type "id".

It only seems fine with a property element.

I am trying the same thing which is to force the (Oracle) database to generate the default value which is a timestamp.


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