Pracede
Pracede

Reputation: 4361

How to modify existing entity generated with jhipster?

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

Upvotes: 52

Views: 69678

Answers (7)

Demobilizer
Demobilizer

Reputation: 748

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.

Upvotes: 5

Martin Naughton
Martin Naughton

Reputation: 1556

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

Upvotes: 4

ThanhLD
ThanhLD

Reputation: 674

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

Upvotes: 32

Venjal
Venjal

Reputation: 61

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.

Upvotes: 0

J&#246;rg
J&#246;rg

Reputation: 2461

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.

Upvotes: 7

Roberto
Roberto

Reputation: 4869

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

Upvotes: 64

brevleq
brevleq

Reputation: 2141

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.

Upvotes: 8

Related Questions