-->
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.  [ 6 posts ] 
Author Message
 Post subject: How to handle not-null column with default value?
PostPosted: Fri Feb 12, 2010 3:40 pm 
Newbie

Joined: Wed Sep 23, 2009 3:56 pm
Posts: 4
We running on an Oracle 10g database and many of our columns are set up such that the value cannot be null but a default value is defined:
Code:
MYCOL VARCHAR2(1) default 'N' not null
DATE_CREATED DATE default sysdate

As I'm working with hibernate in my code I create an entity object and set some of the field values. I'd like Oracle to use the defaults for both of the columns I listed above columns so I don't set the values, thinking the defaults will be used. No, instead I get this error:
Code:
org.hibernate.PropertyValueException: not-null property references a null or transient value: com.mycompany.myentity.dateCreated

So my question is, what is the best practice to tell Hibernate to allow the database to use the default value for columns?

Edit:
I should add that we're using Hibernate 3.3.2 with Annotations


Top
 Profile  
 
 Post subject: Re: How to handle not-null column with default value?
PostPosted: Fri Feb 12, 2010 4:07 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
http://docs.jboss.org/hibernate/core/3.3/reference/en/html_single/#mapping-declaration-class

Code:
<class .. dynamic-insert="true">
</class>


^^ Will hurt performance as Hibernate cannot determine the SQL statement up front anymore; it must be calculated for each insert


Top
 Profile  
 
 Post subject: Re: How to handle not-null column with default value?
PostPosted: Fri Feb 12, 2010 4:14 pm 
Newbie

Joined: Wed Sep 23, 2009 3:56 pm
Posts: 4
Hurts performance...doesn't sound good. So, how do most people handle this situation? Just set the field to whatever the database default is set to before inserting it?


Top
 Profile  
 
 Post subject: Re: How to handle not-null column with default value?
PostPosted: Sat Feb 13, 2010 10:45 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
If there's a database default, won't it just take that when the null is sent to the database?

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject: Re: How to handle not-null column with default value?
PostPosted: Sat Feb 13, 2010 11:03 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
@justin - well what other option is there? It's that or as you said duplicate the 'default values' yourself in the java code

@cameron - no that's not the case because an explicit null is still an explicit value. The default values only 'kick in' as implicit values


Top
 Profile  
 
 Post subject: Re: How to handle not-null column with default value?
PostPosted: Sat Feb 13, 2010 3:40 pm 
Newbie

Joined: Wed Sep 23, 2009 3:56 pm
Posts: 4
steve wrote:
@justin - well what other option is there? It's that or as you said duplicate the 'default values' yourself in the java code

Don't know what other option there is...that's why I'm here. :) Thanks for the help/info, I appreciate it!


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