How to modify existing entity generated with jhipster?
Asked Answered
R

7

52

I use jhipster generator to create a projet. I've generated some entity manually following the information provided in jhipster documentation :

If you prefer (or need) to do a database update manually, here is the development workflow: Modify your JPA entity (add a field, a relationship, etc.) Create a new "change log" in your src/main/resources/config/liquibase/changelog directory. The files in that directory are prefixed by their creation date (in yyyyMMddHHmmss format), and then have a title describing what they do. For example, 20141006152300_added_price_to_product.xml is a good name. Add this "change log" file in your src/main/resources/config/liquibase/master.xml file, so it is applied the next time you run your application If you want more information on using Liquibase, please go to http://www.liquibase.org.

Here the documentation just mention, how to add!!! What to do if i want to delete a field or a relation between entities (jpa entity) ?

Thanks

Roundhouse answered 29/1, 2015 at 13:45 Comment(0)
T
64

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

2) Build the code.

3) In the root of your project run the command:

yo jhipster:entity entityName

NOTE: this command by default overwrite all your manual changes. I recommend to do a commit on a VCS repository before to run this command to eventually revert any not required change.

4) run

mvn liquibase:diff
Tella answered 28/7, 2015 at 21:24 Comment(7)
Upvoted just for the smart note to commit.. ( early, often )Torso
What should I run instead of mvn liquibase:diff when using Gradle?Workmanlike
gradle liquibaseDiffChangelogRonel
What does mvn liquibase:diff do and why is it needed? I have not run it, and got errors that the checksum doesnt match. Do I need to run this locally, and also on production when deploying?Seducer
Wouter, the Liquibase command creates a database diff file to src/main/resources/config/liquibase folder. Usind these diff files, your app can create a valid schema for itself when one is not found (or do a schema update, when required). You only need the liquibaseDiffChangelog command in development, in other environments, the schema is updated using this diffTailrace
this is a good start but you have to take a good look at what changed (using code repostory). Some changes are just added by jhipster instead of replaced. Like the CacheConfiguration and the posibly the nacvr.component.htmlWiegand
I've got error: Unable to resolve name [io.github.jhipster.domain.util.FixedPostgreSQL10Dialect] when I run mvn liquibase:diffTragedy
C
32

I have solution worked for jhipster 3.4.2, it is the result of me in 1 day, hope it helpful to you :)

  1. Run yo jhipster:entity entityName
  2. Modify entities (add, remove, ...), at the end, jhipster will give question to override file, *IMPORTANT NOTE: select NO for changelog .xml file and navbar.html file.
  3. Back to project and run mvn compile liquibase:diff
  4. Run your application
  5. DONE
Carlicarlick answered 8/7, 2016 at 1:54 Comment(5)
It worked for me too with jhipster 3.9.1. I just had to append the changelog just created in the changelog master (src/main/config/liquibase/master.xml)Aarau
When you just want to change datatype?Hysterectomize
@Hysterectomize then you should modify .jdl file, regenerate entity from the file and jhipster will make relevant changes in your services for you.Raddatz
Ah, too bad this doesn't work for khipster (kotlin version). I keep getting NPM errors..Lafferty
@Lafferty I even don't ever work with khipster before but if you get npm errors, you should check npm first.Carlicarlick
M
8

In the same page you can see this sentence:

If you have choosen to use MySQL or Postgresql in development, you can use the mvn liquibase:diff goal to automatically generate a changelog.

Liquibase Hibernate is a Maven plugin that is configured in your pom.xml, and is independant from your Spring application.yml file, so if you have changed the default settings (for example, changed the database password), you need to modify both files.

Here is the development workflow:

Modify your JPA entity (add a field, a relationship, etc.) Compile your application (this works on the compiled Java code, so don't forget to compile!) Run mvn liquibase:diff (or mvn compile liquibase:diff to compile before) A new "change log" is created in your src/main/resources/config/liquibase/changelog directory Review this change log and add it to your src/main/resources/config/liquibase/master.xml file, so it is applied the next time you run your application If you use Gradle instead of Maven, you can use the same workflow by running ./gradlew liquibaseDiffChangelog, and change the database configuration in liquibase.gradle if required.

So you just need change your JPA entities (remove, add, etc), run mvn compile liquibase:diff, and put the changes in master.xml.

Margeret answered 19/2, 2015 at 11:30 Comment(0)
U
7

You can also modify existing entities interactively, e.g. using yo jhipster:entity Foo for entity Foo.

This way, you can regenerate all entities and dialogs.

If you are using a RDBMS and liquibase, you have to write the changelog file(s) and add it to the master.xml. What helps here is using mvn liquibase:diff or gradlew liquibaseDiff, which you can run against your existing DB. Though, I would recommend to write the changelog files manually.

Unglue answered 28/4, 2016 at 20:59 Comment(1)
Now that everyone is on Jhipster 3.0 (right?) this should be the accepted answerHierarchy
R
5

You can modify entity by tow ways.

First(using jdl-studio):

update .jh or .jdl file by following steps:

1) export .jh file using jhipster export-jdl myApp-jdl.jh command to root folder of your project. Also you can export the .jh file to particular path using jhipster export-jdl myPath/myApp-jdl.jh

2) here, you can edit your existing entities. You can also add new entities.

3) Then save this myApp-jdl.jh file to root folder of your project and run jhipster import-jdl myApp-jdl.jh or you can store it anywhere on your system and just give path to import from! like, jhipster import-jdl myPath/myApp-jdl.jh

Second(using JHipster CLI):

as suggested on official documentation jhipster.tech/creating-an-entity/ you can update your entity with following steps:

1) type same command jhipster entity <my_entity_name> that we use to create entity using CLI. Here <my_entity_name> would be the entity name that we want to modify.

2) it'll give you different options like, re generate the entity, add more fields and relationships, remove fields and relationships etc..

3) make modifications as per our requirement.

NOTE: it will automatically remove the manual changes that you did on pre-built JHipster project!

SOLUTION:

1) commit the code before modifying or adding new entity.

2) when it'll ask you to overwrite the changes to files, you can skip overwrite option as per your requirement. Just a suggestion is not to chage navbar html file.

3) after that, you can open it to Intellij or Eclipse. And then you can modify other changed files by local history. In Intellij, local history will be in VCS menu.

Reverberate answered 15/5, 2020 at 15:52 Comment(0)
C
4

For jhipster 5.7.0 you can run the entity command again and it will ask if you want to modify it.

 % jhipster entity device                                 
INFO! Using JHipster version installed locally in current project's node_modules
INFO! Executing jhipster:entity device
INFO! Options: from-cli: true

Found the .jhipster/Device.json configuration file, entity can be automatically generated!


The entity device is being updated.

? Do you want to update the entity? This will replace the existing files for this entity, all your custom code will be overwritten 
  Yes, re generate the entity 
❯ Yes, add more fields and relationships 
  Yes, remove fields and relationships 
  No, exit
Complimentary answered 26/11, 2018 at 20:58 Comment(0)
B
0

What I've done to modify jhipster entity like a Relationship to add new tables was:

  1. (save project before anything happens :p) yo jhipster:entity TableToEdit and Edit whatever you want.Who to editTableOrAdd
  2. Then add the new tables.(optional)
  3. I had then I just had to do change or remove all the tables of the DataBase, to generate the new fields and relations on the SQL part automatically.

Note: If after this steps, it has any error, serverError or something I regenerate de entityregenerate Entity. Of course if you want to keep all your data this is not a good alternative.

Busy answered 18/5, 2016 at 9:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.