Search code examples
javaspringdesign-patternsentitypojo

Simple logic in Spring Entity getter?


I have a User entity which has a one-to-many relationship with UserSkill entity. UserSkill can be active and inactive (inactive is set upon skill deletion, instead of deleting it completely). The thing is, that most of the time active skills are important for end user (inactive may be used later for statistical purposes). Having that in mind, when I query User entity from the database, most of the time I want to access only active skills through it.

My question is, which implementation is better:

  1. I have getUserSkills (simple getter) and getActiveUserSkills (getter with some filtering) on the User entity, and use getActiveUserSkills most of the time. (Everyone seems to keep telling that POJO should be POJO, no additional logic.)
  2. I have userSkillService, and I have getActiveUserSkills(User user) method, which returns only active user skills. I then use these active skills along with User when I need them. The problem here is that every time I get User, I will have those unnecessary inactive skills with it.

Other implementation suggestions are also welcome. Thanks !


Solution

  • Of course the first solution is better. There's no reason to use a service when the entity can simply provide the information without needing any additional dependency.

    Using an external service would be useful if you needed access to repositories or other services in order to determine which skills are active (because you generally don't want your entity layer to have a dependency on services and repositories). So, I would say to go with the service or repository way if loading all the skills caused a performance or memory problem, and you hus wanted to execute a query to only get the few active skills among the many skills. But that doesn't seem to be the case here.