-->
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.  [ 2 posts ] 
Author Message
 Post subject: Use database DEFAULT value in a property before INSERT
PostPosted: Fri Feb 29, 2008 2:57 pm 
Newbie

Joined: Fri Feb 29, 2008 1:07 pm
Posts: 5
In Hibernate 3.0, isn't there a way to not include a property at runtime (e.g. when this property is NULL in our app) so the value is set by the database default value and then the persistent object is updated with the generated value (like for the ID) ?

I've got the following default value for a "date" column in my database (timestamp in Hibernate mapping):
Code:
TO_DATE('3112' || TO_CHAR(SYSDATE, 'YYYY'), 'DDMMYYYY')

... and as the column is NOT-NULL, I'd like that Hibernate exclude this column/property/mapping from the INSERT or UPDATE statements IF the transient object property is NULL.

I tried the following mapping without success:
Code:
      <property name="myDate" type="timestamp">
         <column name="MY_DATE" not-null="true"
            default="TO_DATE('3112' || TO_CHAR(SYSDATE, 'YYYY'), 'DDMMYYYY')" />
      </property>


Either I get the following Hibernate exception:
Code:
org.hibernate.PropertyValueException: not-null property references a null or transient value: com.mycompany.somepackage.model.MyObject.myDate


... or if I set not-null="false" , I get the SQL exception:
Code:
org.hibernate.util.JDBCExceptionReporter | ERROR | ORA-01400: cannot insert NULL into ("SCHEMA"."T_MY_TABLE"."MY_DATE")


How can I solve this...?

Cheers,
Maxx


Top
 Profile  
 
 Post subject: Re: Use database DEFAULT value in a property before INSERT
PostPosted: Fri Feb 29, 2008 3:18 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
what if you add generated="insert" to the property?


Farzad-


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