|
|
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..
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 =gt; UserDao =gt; UserDaoImpl =gt; UserService =gt; UserServiceImpl =gt; 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 =gt; ProductDao =gt; ProductDaoImpl =gt; ProductService =gt; Product ServiceImpl =gt; ProductController
Comments =gt; CommentsDao =gt; CommentsDaoImpl .............etc
Friends =gt; ............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 ( quot;userDetailsquot;, user);
model.put (quot;productListquot;, products);
model.put (quot;commentListquot;, comments);
model.put (quot;friendsListquot;, 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 Listlt;HomeDatagt; listHomePageData(int userId) { String query = quot;SELECT U.USERNAME .....quot;; return getHibernateTemplate().find(query, userId);
}
But now I would have to make a HomeData???Returning all these into listCode:
@Autowired
private HomePageService homePageService;
@RequestMapping(method=RequestMethod.GET)
public void listUserDeatils(@RequestParam(quot;idquot;) Integer id, Map lt;String,Objectgt; model) { model.put(quot;allDataquot;, 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
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.
Originally Posted by Marten Deinum
You can wire as much as you want to a Controller..
So your telling me that I can do something like thisCode:
@Controller
public class HomePageController {
@Autowired private UserService userService; @Autowired private ProductService productService; @Autowired private CommentsService commentsService; @Autowired private FriendsService friendsService;
...... @RequestMapping(quot;/home.htmlquot;) public ModelAndView homePageData(@RequestParam(quot;idquot;) Integer userId,fromServletRequest request, fromServletResponse 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 (quot;userDetailsquot;, user); model.put (quot;productListquot;, products); model.put (quot;commentListquot;, comments); model.put (quot;friendsListquot;, friends);
.... }
I've never seen an example with Multiple services @Autowired into a controller? So is this possible?
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...
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! |
|