title: controllers, entity classes or dao - what goes where?
body: <p>With the introduction of Hibernate in my project, my code started getting really coupled, and boilerplate in many places (and it should be the other way round, right?)</p>
  
  <p>I got pretty confused by a particular example. I've always considered DAO objects to be pretty generic in their nature (mostly encapsulating the basic CRUD oeprations as well as the backend storage implementation)</p>
  
  <p>Unfortunately, as my entity classes started to get more complicated, I started offloading more and more logic to the DAO objects. I have a particular example:</p>
  
  <p>my entity class User should have a relation called friends, which is essentially a collection of users. However, I have to map my class to a collection of UserFriendship objects instead, each of which contains a ref to the friend object, but also other specific friendship data (the date when the friendship occurred)</p>
  
  <p>Now, it is easy to introduce a custom getter in the entity class, which will take the collection of UserFriendship objects and turn it into a collection of User objects instead. However, what if I need only a subset of my friends collection, say, like in paging. I cannot really do that in the entity object, because it doesn't have access to the session, right? This also applies to when I need to make a parametrized query on the relationship. The one that has the access to the session is the UserDAO. So I ended up with this</p>
  
  <p>UserDAO
     => normal CRUD methods
     => getFriends(Integer offset, Integer limit);
     => a bunch of similar getters and setters responsible for managing the relationships within the User instance.</p>
  
  <p>This is insane. But I cannot really do anything else. I am not aware if it is possible to declare computed properties within the entity classes, which could also be parametrized. </p>
  
  <p>I could technically also wrap the DAO within the entity, and put the helper getters and setters back into the entity class, where they should be, but I am not sure whether if that is a good practice as well. </p>
  
  <p>I know that the DAO should only be accessed by the controller object, and it should provide a more or less complete entity object or a set of entity objects.</p>
  
  <p>I am deeply confused. More or less all of my DAO objects now couple logic that should be either in the Entity objects or in the controllers.</p>
  
  <p>I am sorry if my question is a bit confusing. It is a bit hard to formulate it.</p>