aspose file tools*
The moose likes Object Relational Mapping and the fly likes Handling Null values in Java Primitive DATA types Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Handling Null values in Java Primitive DATA types" Watch "Handling Null values in Java Primitive DATA types" New topic
Author

Handling Null values in Java Primitive DATA types

Vijay Gadde
Ranch Hand

Joined: Sep 30, 2008
Posts: 54
Hi There, we got a strange scenario in our Hibernate based web application.

On the Database SQL Server 2005,there is a field called user_type of Boolean type and it will store True or False messages and this Data Base SQL Server2005 allows to store "NULL" into this field which is very strange.
When run a HQL Query and set these values into Bean Hibernate will through exceptions saying

I am not sure how to handle this scenario.
In the first case how come DB SQL Server2005 is allowing to store "NULL" values into Primitive types and Hibernate/Java or any programming language cannot/impossible to store NULL into primitive data types.
Any arguments/answers please.
Thanks in advance,VijayGadde


Vijay Gadde
Coming together is a beginning. Keeping together is progress.. Working together is success........
Kumar Subramanian
Greenhorn

Joined: Oct 01, 2008
Posts: 8
You can consider using wrappers for mapping.


Subramanian, Kumar [kumar@eminenttech.com]<br />J2EE Architect, <a href="http://www.eminenttech.com" target="_blank" rel="nofollow">Eminent Technology Solutions (ETS)</a><br />Software / Portals / Alfresco / Outsourcing / Proteomics
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


On the Database SQL Server 2005,there is a field called user_type of Boolean type and it will store True or False messages and this Data Base SQL Server2005 allows to store "NULL" into this field which is very strange.

Why is that strange? Presumably if you want to use a boolean type with a nullable field then the value can be null?

As a general rule, if you have mapped a primitive type to any field that is nullable your mapping is wrong.
[ October 15, 2008: Message edited by: Paul Sturrock ]

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Vijay Gadde
Ranch Hand

Joined: Sep 30, 2008
Posts: 54
Thanks for that postings..!

Now I had Fixed this NULL issues in my application.

If one declares these int,bit,char as Primitive data types in POJO classes.. then we cannot do getter/setter NULL value into this attributes.. but on you these values will allow NULL to store.. so to avoid this conflicts its always suggestible to use WRAPPER classes itself not Primitive types.

What I had observed.. On the DB we have inter, bit types.

How do we need declare them in our Hibernate POJO classes

> Point1: We would need to initialize these variable with WRAPPER classes only
like int - "Integer"
bit - "Boolean"
char - "Character"

does any one dis-agree with this, any comments/suggestions please..!
Amit K Srivastava
Greenhorn

Joined: May 31, 2012
Posts: 7
Hi,
I am getting same problem .
FATAL [reviewBoard] Error in getReviewBoardDetails() in ReviewBoardAgentBeanjavax.persistence.PersistenceException: org.hibernate.PropertyAccessException: Null value was assigned to a property of primitive type setter of com.velos.eres.business.reviewboard.impl.ReviewBoardBean.reviewboardAccId

In this case, i am trying to get name of all Review Board Names.When i click on records inserted by form , i am able to get those records Name , but when i am trying to get records that were inserted in table as seed data.I am getting this error?
I am using "int" in my "ReviewBoardJB.java" file. I am not getting if this is error due to primitive data-type, then why it work with others that records inserted by form.
Please guide me how to solve this.

Thanks,
Amit Kumar Srivastava

Renju Jose
Greenhorn

Joined: Apr 01, 2013
Posts: 1

Seems there is null values when you retireve from the table and hence the error, whereas when you enter from the form you enter values for all the fields. Use wrappers for all primitives in your class.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Handling Null values in Java Primitive DATA types