-->
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: Nullable foreign key and hibernate get method issue
PostPosted: Thu Jul 20, 2006 1:06 am 
Newbie

Joined: Thu Jul 20, 2006 12:30 am
Posts: 2
Hi, I am getting a null object from the get method of the hibernate session class.
Code:
hibSession = getSession();
            retval = hibSession.get(
                    getPojoObj().getClass(), pk);

Before presenting the problem, here is a quick background about my code and database schema.

I have an employee and a department class. Employee class has a field deptno which [nullable] foreign key and referring to the deptno of department table. Since deptno is a nullable a foreign key, I can always create an employee without any department assigned to him. After successful creation of an employee, I now want to assign him a department from the allocate department screen where I get employee information and a drop down list of available departments.

In the web layer, I am using jsf and I have a form bean named EmployeeFormBean. Instead of creating all the proerties in the EmployeeFormBean, I have simply put the object of Employee POJO having all the fields which are mapped to the datbase and a department object as well. The sole reason of doing this was everytime I want to save any information or get some data from the datase, I dont need to populate the formbean properties/ pojo properties again and again. {This might not be a good approach as it makes your middle layer and web layer tightly coupled. Point me in right direction if this is not a right way.}

Problem :- Having done all that, when i try to find any employee by primary key using get method of hibernate session object, I get the the employee pojo which has a null department pojo (if the emp is not assigned any department). As I have put the emp pojo in the EmployeeForm bean, in the jsp page i get employee's deptno by typing :--

Code:
${employeeFormBean.departmentNumber}

EmployeeFormBean has a method called
getDepartmentNumber()
{
    employeePOJO.getDepartmentPOJO().getDepartmentNo();
}


Above code throws a null pointer exception as department is null.

Is there any way by which when I can configure the hibernate so that hibernateseesion.get method
class.
Code:
hibSession = getSession();
            retval = hibSession.get(
                    getPojoObj().getClass(), pk);


returns me a employee object which a department object (containing nothing) instead of a null department object.

One simple way could be -- having all the properties in the formbean and populating the form bean everytime we get a employee pojo which would be a tedious job.

Thanks in advance!!

sachin


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 20, 2006 2:11 am 
Newbie

Joined: Tue Mar 22, 2005 6:01 pm
Posts: 13
Location: Sydney, Australia
Hi Sachin,

The problem here is that you are calling a method on an object which you already know MIGHT be null. Since you have designed the employee class to possibly have a null dept then you need to handle that possibility everytime you access your dept reference.

So you should never do something like getDepartmentPOJO().getDepartmentNo();

rather do
if(getDepartmentPOJO()!=null) {
//do dept stuff here
}

Hope that makes sense.

Cheers
Johnny


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.