Reputation: 9094
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) ?
Upvotes: 5
Views: 16283
Reputation: 12215
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.
Upvotes: 2
Reputation: 2085
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.
Upvotes: 26