I'm using the DAO pattern in a Hibernate project, so I have a Hibernate DAO that implements the standard DAO methods: new, fetchAll, fetch(ID), save(model), update(model), delete(model).
I have a model called User that's linked to many Account models. I'd like to do is keep any HQL in the DAO's themselves, but I also would like to calculate the total balance of a User's attached accounts.
This would be the ideal pattern:
Code:
User user = userDAO.fetch( ... );
user.getTotalBalance() // sums the balances in the user's accounts, preferably completely in the database
Is there anything in Hibernate mappings that I can use to map to a calculated value, e.g. (SELECT SUM(a.Balance) FROM Account a WHERE a.UserID = User.ID)? I suppose my other alternative is to create a getTotalBalance( User user ) in UserDAO, but that doesn't seem like the cleanest solution.[/img][/list][/code]