Announcement Announcement Module
Collapse
No announcement yet.
Multiple domain objects(or Dao's) for a single controller view? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Multiple domain objects(or Dao's) for a single controller view?

    Hey ok so I'm kind of relighting a previous thread (Quite old 2004).
    I posted on it, but yet no record..

    http://forum.springframework.org/showthread.php?t=11756

    So I'm starting a new one. Bare with this post
    I'm sure spring users would love to be able to do this in the future.
    I'm using Spring MVC 2.5.6, Hibernate 3.4.0GA and integrating Spring Sercurity 2.0.0 soon. But I'm not to sure on SiteMesh or Tiles?

    So I would like to be able to use multiple domain objects on a single
    controller so i can see different lists associated with the user.
    Like the users details and then the products, friends, comments that are associated to that user on one page.
    -----------------------------------------------------------------------
    ========
    METHOD 1
    ========
    One page: User.html
    -----------------------------------------------------------------------
    On it I would like to call information from 4 controllers based on the userId.

    User => UserDao => UserDaoImpl => UserService => UserServiceImpl => UserController

    call a userId to get a user row

    UserDaoImpl
    Code:
    	
    public User getUser(int userId){
    	return (User)getHibernateTemplate().get(User.class, userId);
    }
    -----------------------------------------------------------------------
    I also have 3 other controllers

    Product => ProductDao => ProductDaoImpl => ProductService => Product ServiceImpl => ProductController

    Comments => CommentsDao => CommentsDaoImpl .............etc

    Friends => ............etc

    Based on the userId I would like to get the associated Objects/Maps from the Product, Comments and Friends tables in the Database.


    From the controllers I would like to be able to do something like
    Code:
    User user = userService.findUserById(userId);
    ..
    Product product = productService.findProductsById(userId);
    ..
    Comment comment = commentService.findCommentsById(userId);
    ..
    Friends friends = friendsService.findFriendsById(userId);
    ...
    ...
    model.put ( "userDetails", user);
    model.put ("productList", products);
    model.put ("commentList", comments);
    model.put ("friendsList", friends);
    But I know thats impossible as You can only wire/@Autowire
    one service object to a Controller.
    -----------------------------------------------------------------------
    ========
    METHOD 2
    ========
    HomePageContoller??
    -----------------------------------------------------------------------
    Make a new controller that ties in all the information I want and
    then send it to the view

    If I was to make a temporary table that got all the user information
    and associated products, comments, friends,

    I could use a HQL/SQL JOIN to form all the information

    I'm not the best at HQL, so i'll give you what I Think it would be like in SQL
    correct me if I'm wrong.

    Code:
    SELECT U.USERNAME, U.FIRST_NAME, U.SECOND_NAME, U.AGE, U.EMAIL, P.TITLE, P.DESCRIPTION, P.DATE, F.FRIENDS, C.COMMENTS
    FROM USERS U, PRODUCTS P, FRIENDS F, COMMENTS C
    WHERE U.ID = P.USER_ID AND U.ID = P.USER_ID 
    AND U.ID = F.USER_ID AND U.ID = C.USER_ID
    AND U.ID = ?
    So '?' would be the userId that I want to find all this information from.

    UserDaoImpl
    Code:
    public List<HomeData> listHomePageData(int userId) {     
            String query = "SELECT U.USERNAME .....";
            return getHibernateTemplate().find(query, userId);
    }
    But now I would have to make a HomeData???


    Returning all these into list

    Code:
    @Autowired
    private HomePageService homePageService;
           
    @RequestMapping(method=RequestMethod.GET)
    public void listUserDeatils(@RequestParam("id") Integer id, 
          Map <String,Object> model) {
    	    model.put("allData", homePageService.listHomePageData(id));
    }
    .
    .
    I don't know if thats the way it's done but Id love to hear some advice.

    Also would executing that query everytime a users calls that page effect the servers preformance .. too much to handle 1000's of times?
    -------------------------------------------------------------------------

    Thanks for reading!

    And any help at all

    Allan

  • #2
    But I know thats impossible as You can only wire/@Autowire
    one service object to a Controller.
    Where do you get this idea?! You can wire as much as you want to a Controller..

    Also if the data is related why isn't it accessible from your user object? Just having no relationship between the different objects isn't really OO. You would only need to retrieve the user and then use the user to retrieve all the other information.

    Comment


    • #3
      Originally posted by Marten Deinum View Post
      You can wire as much as you want to a Controller..
      So your telling me that I can do something like this

      Code:
      @Controller
      public class HomePageController  {
      
          @Autowired
          private UserService userService;
          @Autowired
          private ProductService productService;
          @Autowired
          private CommentsService commentsService;
          @Autowired
          private FriendsService friendsService;
      
           ......
          @RequestMapping("/home.html")
          public ModelAndView homePageData(@RequestParam("id") Integer userId,HttpServletRequest request,
           HttpServletResponse response) throws Exception {
      
          User user = userService.findUserById(userId);
      
          Product product = productService.findProductsById(userId);
      
          Comment comment = commentService.findCommentsById(userId);
      
          Friends friends = friendsService.findFriendsById(userId);
      
          model.put ("userDetails", user);
          model.put ("productList", products);
          model.put ("commentList", comments);
          model.put ("friendsList", friends);
      
            ....
          }

      I've never seen an example with Multiple services @Autowired into a controller? So is this possible?

      Comment


      • #4
        So is this possible?
        As I said yes it is... There is NO limitation and what and how much you can autowire even if you want to autowire 100 services. However just another question WHY? If you modelled your domain model right I would expect that you could do something like this.

        Code:
        User user = userService.findUserById(userId);
        Product product = user.getProducts();
        Comment comment = user.getComments();
        Friends friends = user.getFriends();
        Which would basically mean that you only need to retrieve the user and let lazy loading and some user.friends etc in you jsp take care of the rest...

        Comment


        • #5
          Nice! Thanks Maten
          I only then realised what I was doing. Your so right with the user.getProducts()
          You set me on the right track!

          Comment

          Working...
          X