5

I have an Entity called Users that have login, name and department as fields that are stored in Users table.

My Spring-Boot configuration defines the departments that gives the system manager role for users in this application.

The key problem is that I want the login end-point to return the User data with the additional information that he/she is or is not a system manager.

I think that this information should be in the User class, but I do not want this information to be stored in the database along with other fields as the management departments may change or the user my change from one department to another that does not manage the system.

(Edit) I will define this IsManager field when user request login(): I will get user's department field and check against managerDepartment's list to set it to true ou false.

So the question is if it is right to put that information in a non-persistent field in the entity class (and how to do that) or if I may change the ResponseEntity in the rest method to add the additional information (and how) ?

NaN
  • 8,596
  • 20
  • 79
  • 153

2 Answers2

24

Short answer is to use @Transient annotation. Correct answer is to separate UserEntity and UserDto to two different classes and use MapStruct for example for transformation.

Andriy Slobodyanyk
  • 1,965
  • 14
  • 15
  • How do you join ORA error and Transient annotation?! "Table must be emtpy" means what it says or newly added column should have default value. – Andriy Slobodyanyk Nov 13 '17 at 14:30
  • @PSyLoCKe Thank you for that, however, I'm still recommending you to follow the second sentence of my answer. – Andriy Slobodyanyk Nov 13 '17 at 16:42
  • As described [in this answer](https://stackoverflow.com/a/30372930/540725) there may be additional Annotations required if yo're going for the quick & dirty `@Transient` solution. – N4ppeL Sep 26 '19 at 13:04
2

Well, this seems to be a question of preference / opinion hopefully still not closed because of that.

It does not make any harm to add @Transient fields but still my preference is to avoid changing @Entities like this.

So in a quite similar case I have decided to create a wrapper Response class that holds the User and stuff like "indirect roles" like system manager role.

Mostly this is because I have all entity classes in a separate project/jar that is then used in various clients & services. Then before adding anything in @Entity I analyze if any other dependent code need this stuff or is it related only to this service, for example.

And if the addition is related only to this specific service/client I do not want to add it into @Entity and make it "more complex" because of this service needs only.

So i think my answer is included quite much the previous paragraph.

pirho
  • 11,565
  • 12
  • 43
  • 70
  • The question is directed to change Entity or ResponseEntity, so I think your answer is valid. The only problem is that you did not answered HOW to do it. So it may be considered only half of the answer. – NaN Nov 13 '17 at 14:07
  • @PSyLoCKe Yes it seems that I happily omitted part **_(and how)_** . But I am not sure now where you have the problem, creating the wrapper class, returning such as a `ResponseEntity` or what? – pirho Nov 13 '17 at 14:12
  • I didn't understood the "wrapper Response" part. – NaN Nov 13 '17 at 14:17
  • 2
    @PSyLoCKe it is just a wrapper class say `UserInfo` that holds both the `user` & boolean `isManager` and that is then returned instead of plain `User` . But because I'm not enough familiar with Spring and you have not provided any actual code how the response is now constructed in details I can not do much. And if I go guessing it is like this `ResponseEntity` => `ResponseEntity` – pirho Nov 13 '17 at 14:23