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