12

I want to add some attributes to the user entity, when I googled about it, I found a similar question :

How to modify existing entity generated with jhipster?

when I followed the steps in this post, I couldn't find the file user.json anywhere as @Roberto montioned

1) Edit the json file representing your entity (add/remove field, the syntax is pretty easy, check in the end of the file if is required any change to the general entity properties like 'fieldsContainOneToMany'...), you'll find it in:

<jhipster_root_folder>/.jhipster/entityName.json

How can I solve this ?

Community
  • 1
  • 1
Renaud is Not Bill Gates
  • 1,684
  • 34
  • 105
  • 191

4 Answers4

14

I think the best solution is a compromise between the two solutions offered by @Pedro and @alcuvi (who references to the JHipster Documentation):

  1. First, create an "ExtendedUser" entity with the additional fields (don't forget to use git, you will have to undo this/delete the entity). A one-to-one relationship to "User" is not necessary.

  2. After that, you can copy many parts from "ExtendedUser" to the other parts of the JHipster Application:

    • Liquibase changelog columns (also add them to users.csv)
    • ExtendedUser.java → User.java and UserDTO.java
    • extendedUser-dialog.* → register.html/.controller.js and settings.html/.controller.js
  3. Adapt AccountResource.java and UserService.java (and UnitTests if you use them). This step is mostly done by using getters and setters copied in the step before. JHipster Documentation (https://jhipster.github.io/tips/022_tip_registering_user_with_additional_information.html) might be helpful here.

  4. Delete the "ExtendedUser" Entity (using git, or manually, see also: How to delete an entity after creating it using jhipster?)

The *advantages* are:

  • Using JHipster code generation capabilities
  • No additional entity (which comes with new DB tables and many files)

I hope this information will help other developers in the future!

Community
  • 1
  • 1
Domi W
  • 574
  • 10
  • 15
  • 3
    Thanks. This was the solution I used for a different project a couple of years ago, and now I've run into it again. I really thought it would be fixed by now. JHipster team!! Just create the User class through the same JSON mechanism, and then add the extra stuff needed for security! Why did you make this special, and therefore a pain? – Zag Aug 22 '19 at 17:27
  • ..or please give us an option to specify a list of extra fields to be added to the User entity in JDL. – Wolf359 Sep 20 '20 at 17:16
4

The User entity is the entity used by JHipster to manage all user management stuff, like email, passwords, etc., so you won't find a User.json file, since that is an automatically generated entity. Those .json files are only created when you run yo jhipster:entity <entityName>.

In order to add/remove fields to the User entity, you'll have to do it manually, that means editing User.java, creating a liquibase changeset and modify all related files in the UI as needed.

Pedro Madrid
  • 1,887
  • 1
  • 20
  • 32
  • I edited my User.java and frontend files manually as @Pedro suggested. The good thing is that when you update projects by adding new entities, your manual updates don't get overwritten. – Halil Sep 18 '17 at 19:05
2

The official documentation of jhipster (version 4) have an entry about this.

https://jhipster.github.io/tips/022_tip_registering_user_with_additional_information.html

In summary...

Its manual solution. The proposed solution is to create an entity with the fields you want to add to user and linked to it with a one to one relationship.

Alternatively

If you create a ExtendedUser with new fields with JDL-Studio the jhipsterimport-jdl command is going to create "extendedUser" option on entity menu where you can set values for those fields and linked to the user you want.

I thinks is not the best solution...

enter image description here enter image description here

alcuvi
  • 77
  • 5
0

What about inheritance? Just extend built-in User class with new fields. For example, public class ExtendedUser extends User. And replace User class with ExtendedUser in code. Update dto, service, etc. Also you can use class casting where it's required. What thoughts?

Binakot
  • 298
  • 5
  • 15