Hi All,
I have spent quite some time looking into why the OpenSessionInView filter is considered a bad idea. I haven't yet found a definitive answer (or set of answers) as to why this is so, but from what I've read it seems to be considered a bad idea because you're tying the view rendering to the database session, and there is no guarentee that the view (ie - the browser) will finish rendering properly (network connection may die, etc). Are there any other reasons why the OpenSessionInView filter is discouraged?
Now, assuming that OpenSessionInView is a bad idea, I want to redevelop an application I made earlier to use the "correct" approach (ie - by not using the OpenSessionInView filter).
So rather than doing the following in my view:
1. Do I have to change my code to dip into the service layer each time I have to rely on ORM:Code:Employee employee = serviceLayer.getEmployee(); String employerFirstName = employee.getManager().getFirstName();
with my serviceLayer implementation being:Code:Employee employee = serviceLayer.getEmployee(); Manager manager = serviceLayer.getManagerForEmployee(employee); String employeeFirstName = serviceLayer.getFirstNameForManager(manager);
2. Or do I have to preload all values in the service layer and pass "built-up" model back:Code:public class MyServiceLayer { public Employee getEmployee() { return this.employeeDao.readSomeEmployee(); } public Manager getManagerForEmployee(Employee employee) { Employee persistentEmployee = this.employeeDao.readById(employee.getId()); return persistentEmployee.getManager(); } public String getFirstNameForManager(Manager manager) { Manager persistentManager = this.managerDao.readById(manager.getId()); return persistentManager.getFirstName(); } ... }
with my serviceLayer implementation being the following:Code:Employee employee = serviceLayer.getEmployee(); String employerFirstName = employee.getManager().getFirstName();
Or would I do something completely different to get the data loaded in memory?Code:public class MyServiceLayer { public Employee getEmployee() { Employee employee = this.employeeDao.readSomeEmployee(); employee.getManager().getFirstName(); // just read it ... // .. and similarly read all other values I will need later on // such as getLastName(), getAge(), and anything else return employee; } }
I would really appreciate any help. I want to do this properly.
~gautam


Reply With Quote
