-->
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.  [ 8 posts ] 
Author Message
 Post subject: Handling null values in Hubernate
PostPosted: Sat Dec 29, 2007 5:56 pm 
Newbie

Joined: Sat Dec 29, 2007 9:58 am
Posts: 5
Hi

How to handle null values in Hiberante? Can someone provide me an example?

Thanks,
Raj


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 01, 2008 8:13 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
This question does not have enough detail. What do you want to achieve when dealing with null values? Then maybe someone can help you.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 01, 2008 8:21 pm 
Regular
Regular

Joined: Sat Nov 25, 2006 11:37 am
Posts: 72
Hibernate handles NULL values just fine. Typically properties that map into columns allowing NULLs are defined using the appropriate java.lang... types while properties mapping into NOT NULL columns can use the Java basic types. So for example an INT column would be mapped as an Integer property with the Java null value being the equivalent of the SQL NULL value while an INT NOT NULL column would be mapped into an int property.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 07, 2008 12:40 am 
Newbie

Joined: Sat Dec 29, 2007 9:58 am
Posts: 5
iam trying to access all the rows of the EMP table iam getting the following exception.

Hibernate: select emp0_.EMPNO as EMPNO0_, emp0_.ENAME as ENAME0_, emp0_.JOB as JOB0_, emp0_.MGR as MGR0_, emp0_.HIREDATE as HIREDATE0_, emp0_.SAL as SAL0_, emp0_.COMM as COMM0_, emp0_.DEPTNO as DEPTNO0_ from EMP emp0_
Exception in thread "main" org.hibernate.PropertyAccessException: Null value was assigned to a property of primitive type setter of events.Emp.comm
at org.hibernate.property.BasicPropertyAccessor$BasicSetter.set(BasicPropertyAccessor.java:85)
at org.hibernate.tuple.entity.AbstractEntityTuplizer.setPropertyValues(AbstractEntityTuplizer.java:337)
at org.hibernate.tuple.entity.PojoEntityTuplizer.setPropertyValues(PojoEntityTuplizer.java:200)
at org.hibernate.persister.entity.AbstractEntityPersister.setPropertyValues(AbstractEntityPersister.java:3566)
at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:129)
at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:854)
at org.hibernate.loader.Loader.doQuery(Loader.java:729)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
at org.hibernate.loader.Loader.doList(Loader.java:2220)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2104)
at org.hibernate.loader.Loader.list(Loader.java:2099)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:378)
at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:338)
at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:172)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1121)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:79)
at events.Main.loadEmployees(Main.java:92)
at events.Main.main(Main.java:21)
Caused by: java.lang.IllegalArgumentException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.hibernate.property.BasicPropertyAccessor$BasicSetter.set(BasicPropertyAccessor.java:42)
... 17 more


"comm" property has setter and getter methods of type "double"

// the following is the peice of code in my java class

List list = session.createQuery("from Emp").list(); // loads all
// employees

Iterator iter = list.iterator();

while (iter.hasNext()) {
Emp emp = (Emp) iter.next();

System.out.println(emp.getEmpno());
System.out.println(emp.getEname());
System.out.println(emp.getJob());
System.out.println(emp.getMgr());
System.out.println(emp.getHiredate());
System.out.println(emp.getSal());
System.out.println(emp.getComm());
System.out.println(emp.getDeptno());
}


the query is giving all the results when i execute it from the SQL prompt.

// the following is the table definition.

Name Null? Type
----------------------------------------- -------- ----------------------------
EMPNO NUMBER(4)
ENAME VARCHAR2(10)
JOB VARCHAR2(9)
MGR NUMBER(4)
HIREDATE DATE
SAL NUMBER(7,2)
COMM NUMBER(7,2)
DEPTNO NUMBER(2)


How do we handle null values here?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 09, 2008 1:16 pm 
Expert
Expert

Joined: Sat Jan 17, 2004 2:57 pm
Posts: 329
Location: In the basement in my underwear
As per malm66's post above, you would use a Double not double if your property is nullable.

_________________
Some people are like Slinkies - not really good for anything, but you still can't help but smile when you see one tumble down the stairs.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 05, 2009 10:20 pm 
Newbie

Joined: Fri Aug 17, 2007 2:11 pm
Posts: 19
I apologize for using this thread for posting my question. This subject closely matches my current issue.

I am fairly new to Hibernate, so maybe this is an easy one for the pros.

This applies to all my varchar2 (Oracle) fields in my app.

I have a field, mapped as

Code:
   @Column(name = "MODEL_DESCRIPTION")
   public String getModelDescription() {
      return this.modelDescription;
   }

   public void setModelDescription(String modelDescription) {
      this.modelDescription = modelDescription;
   }


This field may have null values.

My Seam page
Code:
     <h:inputText id="itemModelDescription"
                      value="#{itemHome.instance.modelDescription}"/>

other fields ...



If I change any other values on my page with #{itemHomeitemHome.update}, everything works fine. After update modelDescription is still null in the db.


The problem I have is that when I first load my entity and make NO changes, Hibernate issues an unnecessary update.
The update statement I see is
Code:
   update
        ITEM
    set
        DATE_MODIFIED=?,
        VERSION_ID=?,
        MODEL_DESCRIPTION=?
    where
        ITEM_ID=?
        and VERSION_ID=?



date_modified is from MappedSuperClass field


Debugging, it looks like at some points setModelDescription sets "" into the field making Hibernate believe the data is dirty triggering an update.

Thanks in advance for any help.

My first post this year so a very Happy New Year to the Hibernate team and other users!

Franco


Top
 Profile  
 
 Post subject: Re:
PostPosted: Thu Nov 11, 2010 9:34 am 
Newbie

Joined: Wed Oct 27, 2010 8:31 am
Posts: 2
I found this comment from another forum:

"Formulas may not have an unknown (null) value for primitive types. You should use an Object type (Integer, Boolean, etc.) which can be set to null. But int,boolean cannot. "


In my application i changed this:

private int saldo;

public void setSaldo (int i) { saldo = i; }
public int getSaldo () { return saldo; }

that gave the error mentioned, to this


private Integer saldo;

public void setSaldo (Integer i) { saldo = i; }
public Integer getSaldo () { return saldo; }

that works!


Top
 Profile  
 
 Post subject: Re: Handling null values in Hubernate
PostPosted: Fri Dec 17, 2010 12:34 am 
Regular
Regular

Joined: Sun Apr 13, 2008 3:04 am
Posts: 71
Location: Bangalore
Channing the object variable type to from primitive type to respective Object type is enough. One need not change setter and getter methods..

change
private int saldo;

to
private Integer saldo;

_________________
Raja Nagendra Kumar,
C.T.O
http://www.tejasoft.com
TejaSoft - Specialists in Code Audit, Unit Testing and Merciless Re-factoring - Engineering Crisis Turnaround Experts


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