Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a non-persistent Field in an JPA Entity?

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) ?

like image 247
NaN Avatar asked Nov 28 '22 22:11

NaN


1 Answers

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.

like image 57
Andriy Slobodyanyk Avatar answered Dec 04 '22 01:12

Andriy Slobodyanyk