reset password
Author Message
lbriggs
Posts: 57
Posted 19:26 May 12, 2014 |

So far we have only done a few data base query such as:

public User getUser( Integer id )  {  return entityManager.find( User.class, id );  }

And we placed them inside our UserDaoImpl class. Then in our controller during we grab the user:

User user = userDao.getUser(username);

In assignment 4 I needed a few extra methods regarding user so I created them like the one I just exampled and added them to the ModelMap. But it gets a bit ugly if you are passing all of these attributes regarding a user in the controller. I would like to create methods regarding the user in the user class that access the database. For example, my UserDaoImpl class has the method:

public List<Game> getGamesAgainstAI(User user) { .... }

I wold like to place that in the User class itself, but when I attempted it, there were errors about the AutoWiring of the Beans. What is the recommended design pattern to follow? Create methods in the User class and pass a single User in the controller. Or the UserDAO class and pass them as separate objects to the ModelMap in the controller?

Thanks,

Loran

cysun
Posts: 2935
Posted 20:46 May 12, 2014 |

If I understand what you are trying to do -- the best way to do It is to change your model design. For example, if you design your User class like this:

class User {
...
List<Game> gamesAgainstAI;
}

Then you don't need to create any DB access code as you can simply do user.getGamesAgainstAI() (and ORM will automatically load the data from database). And in controller you can just pass a User object to view, and in JSP you can do ${user.gamesAgainstAI}.

lbriggs
Posts: 57
Posted 23:47 May 12, 2014 |

Yes before I didnt have proper bi-directional relationship so I had to hack it together. I set it up better now so the User object contains all the information I need.

Thanks