-->
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: null values in primitive types
PostPosted: Tue Feb 10, 2009 3:40 pm 
Newbie

Joined: Tue Feb 10, 2009 3:31 pm
Posts: 3
Hello,

I'm getting Property Access Exception "Null value was assigned to a property of primitive type" when I try to use session.find.

I have primitive-type members in my java classes, and related fields in the database tables can have null values.

I wonder is this allowed or not?
and if it's not allowed the only way to overcome this problem is to convert primitive fields to wrapper types (e.g. long to Long). (I cannot change database structure for now)
Right?

_________________
garage door opener installer aurora
custom awards


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 11, 2009 8:15 am 
Regular
Regular

Joined: Thu Sep 06, 2007 2:22 am
Posts: 108
Location: Noida,India
You are right.
This error occurs when hibernate try to fill the object and it try to assign the null value to the primitive type of property.

User wrapper class instead of primitive data types to overcome this problem.


Top
 Profile  
 
 Post subject: Re: null values in primitive types
PostPosted: Wed Jan 13, 2010 11:19 am 
Pro
Pro

Joined: Mon Apr 16, 2007 8:10 am
Posts: 246
But then, if one has a table with allowed null values, but still thinks of storing only 0 or 1 values, how to make sure to retrieve a 0 if the stored value is accidently null ?

Note that I can't change the table structure.


Top
 Profile  
 
 Post subject: Re: null values in primitive types
PostPosted: Thu Jan 14, 2010 3:26 am 
Regular
Regular

Joined: Thu Sep 06, 2007 2:22 am
Posts: 108
Location: Noida,India
@stephaneeybert :-you can do that by setting the value 0 when encounted "null" in setter method.. But for that you have to make change in your configuration so that hibernate use "method" instead of "field" to set/get the values. for example :-
Code:

@Column(name="value")
public void setValue(Integer value){
if(value == null)
    this.value = new Integer(0);
else
    this.value = value;
}


Top
 Profile  
 
 Post subject: Re: null values in primitive types
PostPosted: Sun Jan 17, 2010 1:04 pm 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Also, setting defaults at the database level can help to avoid this as well.

_________________
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  
 
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.