First of all, I'd like to thank "Paul Etienne" for his helpful question and answer! The answer bellow is a little bit more detailed and has been written considering the last version of JHipster (3/17/2018).
The question is clear: how to add new data field(s) to User entity in a JHipster project.
As it's mentioned in the official JHipster website, the best way to add new fields/relations to the default JHipster User is creating a new Entity and link it to User with a one-to-one relationship. You may handle more relationships in the new Entity. Let's call this new Entity UserExtra.
The UML diagram above, shows a composition between User and UserExtra meaning that a User has a UserExtra which is strongly dependent to the User and cannot exist without the User. In other words, there should be a user first so we can assign a phone number to.
Step 1: Creating a new entity
I suggest creating the new entity using terminal: jhipster entity userExtra
or
JDL:
Step 2: Modifying the new entity
You may find the new entity class under the domain folder:
We may map the id of UserExtra to the id of User so we can use the id of UserExtra as the foreign-key. If so, then we should not use @GeneratedValue annotation for id in UserExtra anymore. Here is a sample UserExtra class. Here is a sample of a modified UserExtra:
Step 3: Modifying the configuration of the new entity
[You may skip this step if you've used JDL in the 1st step]
There is a json file keeping the configuration of the entity: We need to modify the configuration. here is a sample of a modified configuration json file:
There are also some xml change logs: Here are sample change logs:
added_entity_UserExtra:
added_entity_constraints_UserExtra:
Just be aware that JHipster uses a table named "JHI_USER" for the User entity and has created a table named "USER_EXTRA" for the new entity. We are going to name the primary-key/id field of the new entity "USER_ID":
Step 4: Modifying ManagedUserVM.java
ManagedUserVM class is sort of a DTO extending UserDTO which is used in the REST controller.
The purpose of this modification is adding the new field(s) we are going to bind to User as extra information. Here is a sample code of this class. The phone attribute has been added to the modified class:
Step 5: modifying UserService.java
There is a method named "registerUser" in this calss which is responsible for registering a new user:
The purpose of modifying this service class is to force it to also add a UserExtra object when a User is getting added. here is a sample of a modified "registeruser" method. You may find the added code with "Create and save the UserExtra entity" at top of it as comment:
Step 6: modifying AccountResource.java
This class is a REST controller responsible for Account related activities:
There is a method named "registerAccount" in this controller that calls "registerUser" method of UserService class:
We need to modify this line so we can pass the new field(s) to the method:
Step 7: Modifying the user interface
Finally, you need to add an input element into the html file:
Then, this is what you'll have in the registration page:
And here is the data layer:
JHI_USER
USER_EXTRA
P.S.
The easiest way to do this is just adding a UserExtra entity with a one-to-one relation to User via JDL and adding a "userExtra" while creating a new user.