2013-05-06 98 views
0

我有一個對象,我想要檢索,而不僅僅是對象,但關聯的子對象。彈簧 - 將對象投出對象

控制器,其中,如果我做這樣的事情:

  List<UserEntity> u = (List<UserEntity>) userService.getUserByID(userId); 
     List<UserEntityDTO> userDTO = (List<UserEntityDTO>) new UserEntityDTO(); 

     for (UserEntity user: u) { 


      UserEntityDTO dto = new UserEntityDTO(); 

      dto.setUserId(user.getUserId()); 
      dto.setsModule(moduleService.getSetterModules(user.getUserId())); 
      dto.setcModule(moduleService.getCheckerModules(user.getUserId())); 

      userDTO.add(dto); 

     } 

     map.addAttribute("setter", userDTO); 

存在錯誤

java.lang.ClassCastException: com.myproject.app.model.UserEntity cannot be cast to java.util.List 
com.myproject.app.controller.UserController.getSetPage(UserController.java:320) 
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
java.lang.reflect.Method.invoke(Method.java:601) 
org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:219) 
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132) 
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104) 
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:746) 
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:687) 
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80) 
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925) 
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856) 
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:915) 
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:811) 
javax.servlet.http.HttpServlet.service(HttpServlet.java:621) 
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:796) 
javax.servlet.http.HttpServlet.service(HttpServlet.java:728) 
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330) 
org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118) 
org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84) 
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113) 
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103) 
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113) 
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54) 
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45) 
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilter(BasicAuthenticationFilter.java:150) 
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
org.springframework.security.web.authentication.ui.DefaultLoginPageGeneratingFilter.doFilter(DefaultLoginPageGeneratingFilter.java:91) 
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:183) 
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105) 
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87) 
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192) 
org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160) 
org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346) 
org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259) 

我想一些洞察獲取對象和關聯的持久組的正確方法。或者是有另一種更直接的方式,那麼我會欣賞指針。

+0

哦,男孩,又一個又長又無用的堆棧跟蹤......(我在處理我的代碼時看到很多這樣的人,而且幾乎總是框架底部的框架完全沒用,除了偶爾遇到某些事情時非常錯誤的框架本身。) – 2013-05-06 07:06:35

回答

0

我認爲錯誤是在這條線拋出:

List<UserEntity> u = (List<UserEntity>) userService.getUserByID(userId); 

userService.getUserByID(userId)可能返回UserEntity單個實例。所以,如果你想有一個列表,你必須把它包起來是這樣的:

List<UserEntity> u = new ArrayList<UserEntity>(); 
u.add(userService.getUserByID(userId)); 

同樣的問題也出現在第二行,你試圖通過亞歷克斯指出投下的UserEntityDTO實例List。你必須把它包起來以同樣的方式:

List<UserEntityDTO> userDTO = new ArrayList<UserEntityDTO>(); 
userDTO.add(new UserEntityDTO()); 

但我懷疑你真的想要的UserEntityDTO增加新的空實例到列表中,那麼可以跳過第二行。
我也懷疑你真的需要在列表中包裝這些實體和DTO對象。

+0

我已經完成了。非常感謝。這幾天來一直是個問題。爲了獲得關聯的對象,我確實需要將DTO對象包裝到列表中。非常感謝。 – user2259555 2013-05-06 07:12:47

0

這是不正確的:(List) new UserEntityDTO()
應該

List<UserEntityDTO> list = new ArrayList<UserEntityDTO>(); 
list.add(new UserEntityDTO()); 
+0

謝謝你爲我提供列表上的指針。我很感激。 – user2259555 2013-05-06 07:13:27