Announcement Announcement Module
Collapse
No announcement yet.
Dynamic Web Remoting Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Dynamic Web Remoting

    Hi guys,

    I've got DWR integrated with my spring / hibernate project. But for some reason after re-factoring code I get a lazy loading exception to some completely ORM mapping!

    Before - Working

    Code:
    @Controller
    @RequestMapping("/timetracking.htm")
    public class TimeTrackingController {
    	
    	private static Logger log = Logger.getLogger(TimeTrackingController.class);
    
    	private UserDAO userDao;
    	private TimeTrackingBean timetrackingBean;
    	
    	
    	@Autowired
    	public TimeTrackingController(UserDAO userDao){
    		this.userDao = userDao;
    	}
    	
    	@RequestMapping(method = RequestMethod.GET)
    	public String showTimetrackingForm(ModelMap model) {
    		User user = userDao.findById(UserManager.getCurrentUser().getId(), true);
    		timetrackingBean = new TimeTrackingBean();
    		Collection<Project> projects = new ArrayList<Project>();
    		for (ProjectUser projectUser : user.getProjectUser()) {
    			projects.add(projectUser.getProject());
    		}
    		timetrackingBean.setAssignedProjects(projects);
    		timetrackingBean.setAllAssignedTasks(user.getTasks());
    		model.addAttribute("timetrackingBean", timetrackingBean);
    		return "timetracking";
    	}
    
    	
    	public Collection<SimpleTask> getTasksByProject(long id) {
    		Collection<Task> allAssignedTasks = timetrackingBean.getAllAssignedTasks();
    		Collection<SimpleTask> assignedTasksForSelectedProject = new ArrayList<SimpleTask>();
    		for (Task task : allAssignedTasks) {
    			if (task.getProject().getId() == id) {		
    				SimpleTask simpleTask = new SimpleTask();
    				simpleTask.setId(task.getId());
    				simpleTask.setName(task.getName() + " - " + task.getDescription());
    				assignedTasksForSelectedProject.add(simpleTask);
    			}
    		}
    		return assignedTasksForSelectedProject;
    	}
    }
    After - not working

    Code:
    @Controller
    @RequestMapping("/timetracking.htm")
    public class TimeTrackingController {
    	
    	private static Logger log = Logger.getLogger(TimeTrackingController.class);
    
    	private UserDAO userDao;
    	private TimeTrackingBean timetrackingBean;
    	
    	
    	@Autowired
    	public TimeTrackingController(UserDAO userDao){
    		this.userDao = userDao;
    	}
    	
    	@RequestMapping(method = RequestMethod.GET)
    	public String showTimetrackingForm(ModelMap model) {
    		timetrackingBean = new TimeTrackingBean();
    		User user = userDao.findById(UserManager.getCurrentUser().getId(), true);
    		if (user != null) {
    			TimeTrackingBeanFactory timeTrackingBeanFactory = new TimeTrackingBeanFactory();
    			timetrackingBean = timeTrackingBeanFactory.makeTimeTrackingBean(user, timetrackingBean);
    			model.addAttribute("timetrackingBean", timetrackingBean);
    		}
    		return "timetracking";
    	}
    	
    	
    	public Collection<SimpleTask> getTasksByProject(long id) {
    		Collection<Task> allAssignedTasks = timetrackingBean.getAllAssignedTasks();
    		Collection<SimpleTask> assignedTasksForSelectedProject = new ArrayList<SimpleTask>();
    		for (Task task : allAssignedTasks) {
    			if (task.getProject().getId() == id) {		
    				SimpleTask simpleTask = new SimpleTask();
    				simpleTask.setId(task.getId());
    				simpleTask.setName(task.getName() + " - " + task.getDescription());
    				assignedTasksForSelectedProject.add(simpleTask);
    			}
    		}
    		return assignedTasksForSelectedProject;
    	}
    }
    And here is the JSP

    Code:
    <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
    <%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>
    
    <jsp:include page="fragments/header.jsp" />
    
    <script type='text/javascript' src='/timetracker/dwr/interface/TimeTrackingController.js'></script>
        <script type='text/javascript' src='/timetracker/dwr/engine.js'></script>
        <script type='text/javascript' src='/timetracker/dwr/util.js'></script>
        
        <script type="text/javascript">
        function onChangeProject() {
        	TimeTrackingController.getTasksByProject($("selectedProject").value, populateTasks);
            //StationFinder.getAktivityByProjekt1($("selectedProject").value, populateTasks);
          }
    
        // fills in task values based on Project
        function populateTasks(data) {
          dwr.util.removeAllOptions("taskList");
          dwr.util.addOptions("taskList", data, "id", "name");
        }
               
        </script>
    ${timetrackingBean.username} ${timetrackingBean.company}
    
    
    <form:form method="POST" modelAttribute="timetrackingBean">
    <tr>
          <td>
              <form:select path="selectedProject" id="selectedProject" onchange="onChangeProject()">
                  <form:option value="0" label="Select Project"/>
                  <form:options items="${timetrackingBean.assignedProjects}" itemValue="id" itemLabel="name"/>
              </form:select>
              
              <form:select path="selectedTask" id="taskList">
                 <form:option value="0" label="Select Task"/>
              </form:select>
              
          </td>
      </tr>
      </form:form>
      
      
    
    
      
    
    
    
    HEADING
    
    DATE
    
    PROJECT
    
    ACTIVITY
    
    <jsp:include page="fragments/footer.jsp" />
    I really don't understand why one way works and the other doesn't.

    What on earth i'm I doing wrong?

    Your thoughts?

  • #2
    Can you post the source for TimeTrackingBeanFactory? Also how are you opening your hibernate session? Are you using a OpenSessionInView interceptor or filter?

    Comment


    • #3


      Silly me. I made some modifications to the working code before I posted it.

      Code:
      for (Task task : user.getTasks()) {
      			log.error(task.getProject() + " " + task.getName());
      		}
      I removed this line not thinking it would be important.

      But I guess it loads task object fully by loading tasks collection. So I inserted this line to make it work

      Code:
      Hibernate.initialize(user.getTasks());
      Would it be ok to just use the above code anywhere I need it?

      Comment


      • #4
        It depends upon how you have your application setup. If you are using a service pattern then you would initialize your objects in the service layer. By doing this your controller code does not know that you are using Hibernate. I use Service layers in my web apps all the time and it works very well. If down the road you want to change persistence layers then you just change your service layer and your controller code does not change at all.

        Comment

        Working...
        X