This is a pretty inconvenient situation occurs while using Hibernate ORM for developing Data Access Layer logic in our java application .
Though Lazy loading is considered as one of the greatest advantage given by hibernate while considering the performance still we got situations like the LazyInitializationExceptions.
As lazy loading reduces hits to the database by the principle like "prepare food only when you are hungry , but don't prepare food even though you are not hungry presently . so when you prepare food even though you are not hungry then the food may get waste"
Like wise if you don't require the data urgently then don't go to the data base directly . only go to the database when you need to access the data. this way hibernate reduces unnecessary hits to the database.
Now ,We can consider a situation like
suppose an object employee is being loaded using load() method and also the lazy attribute is configured as true
Employee employee=(Employee)session.load(Employee.class,101);
session.close();
int no=employee.getEmployeeNo();
here getter method getEmployee() is used to get the employeeNo from the employee object but before that the session is closed then we have no Connection to the database and as we know when an object is loaded using the load() method with the lazy attribute equal to true then hibernate returns the proxy class object of Employee instead of real Employee class object from database and the proxy is having only the id assigned to it.
(proxy class> ---$$--javaassist_0 class)
so when we try to access the other properties by calling getter method on the object and before that if the session is closed then hibernate couldn't load the object from the database and it throws
LazyInitializationException .
hope the article is helpful to you
in case any query feel free to comment below..
Though Lazy loading is considered as one of the greatest advantage given by hibernate while considering the performance still we got situations like the LazyInitializationExceptions.
As lazy loading reduces hits to the database by the principle like "prepare food only when you are hungry , but don't prepare food even though you are not hungry presently . so when you prepare food even though you are not hungry then the food may get waste"
Like wise if you don't require the data urgently then don't go to the data base directly . only go to the database when you need to access the data. this way hibernate reduces unnecessary hits to the database.
Now ,We can consider a situation like
suppose an object employee is being loaded using load() method and also the lazy attribute is configured as true
Employee employee=(Employee)session.load(Employee.class,101);
session.close();
int no=employee.getEmployeeNo();
here getter method getEmployee() is used to get the employeeNo from the employee object but before that the session is closed then we have no Connection to the database and as we know when an object is loaded using the load() method with the lazy attribute equal to true then hibernate returns the proxy class object of Employee instead of real Employee class object from database and the proxy is having only the id assigned to it.
(proxy class> ---$$--javaassist_0 class)
so when we try to access the other properties by calling getter method on the object and before that if the session is closed then hibernate couldn't load the object from the database and it throws
LazyInitializationException .
hope the article is helpful to you
in case any query feel free to comment below..
0 comments:
Post a Comment