How to remove Required field from an entity in Jhipster - jhipster

I am new to Jhipster and I want to remove required field from column without changing anything else like
table name in h2 db or the names of the columns in that table. I tried to remove #NotNull annotation from entity name and set field in database to null but when i run integration test using
mvnw verify i get this error
EmployeeResourceIT.checkCompanyIsRequired:124 Status expected:<400> but was:<201>
Thank you in advance!

You must modify the EmployeeResourceIT test also so that it does not verify company requirement anymore.
It would be simpler to modify your JDL file to change the constraint on this field and import it.
And if you did not use JDL, you should learn it, it's more powerful than answering questions (see https://www.jhipster.tech/jdl/getting-started)
You can create your JDL file from your existing project using jhipster export-jdl

Related

Removing an Entity completely from Jhipster built Application

I have a requirement to accompish, which is to delete an entity completely, which has been created using jhipster.
If I use the command "jhipster entity entityName", it gives option to delete fields and relationships only but not complete removal of entity. While doing some research, I came across this link [https://github.com/jhipster/generator-jhipster/pull/4369][1]. But there seems to be no solution to it.
Also if I'm using the above command to remove the fields and relationships with the help of the existing entityName.json file in .jhipster folder to avoid the prompts on cmd, I need to keep the fields and relationships keys in that json file as null or [], right?
Looking for some help on this.Thanks.

Proper procedure for modifying JHipster Entities using JDL

I am trying out JHipster (version 6.4.1) using a Monolith and disk-based H2 database. I have created some entities in JDL and have the basic CRUD webpages working. Now that I feel comfortable with the process, I want to add fields and rename others. I figured I could simply update the JDL, re-import the JDL, start the application, and see the result of my changes. What I see is ValidationFailedException from Liquibase and the application throwing HTTP 500 errors due to database problems.
I have looked all over for guidance on the proper process for handling this seemingly common development scenario. Most of the places I have looked for guidance (such as https://www.jhipster.tech/creating-an-entity) discuss importing JDL as a one-time-only operation and do not discuss how to incrementally change and import the JDL.
I have tried a number of suggestions as seen on SO, such as not overwriting the changelogs, doing a liquibase:diff, and adding that to master.xml. This still causes the ValidationFailedException. In the master.xml I see the comment <!-- jhipster-needle-liquibase-add-changelog - JHipster will add liquibase changelogs here --> which leads me to believe that JHipster should be doing the heavy lifting, but I am just missing a step.
I am by no means a JHipster nor a Liquibase expert, but I want to learn. How I can perform simple entity updates without a huge hassle?
[Update with more detail]
After re-importing the updated JDL, I have managed to get rid of the DB Validation Errors by blowing away the database with rm -rf target/h2db/db.
I'm happy with my changes and feel like a commit is in order. What I see is
master.xml is unchanged
the changelog from the first JDL import has been modified to include the updates I made
If I understand how liquibase works, I would have expected
None of the existing changelogs would be touched
A brand new changelog file would be created that contained just the JDL changes I made this round
master.xml to have changed only in that it would contain an additional changelog entry, pointing to the file created in item 2
Am I misinterpreting how Liquibase represents evolution of the DB schema?
It appears that the page you referenced does have some instructions for updating entities. Farther down the page I saw this:
Updating an existing entity
The entity configuration is saved in a
specific .json file, in the .jhipster directory. So if you run the
sub-generator again, using an existing entity name, you can update or
regenerate the entity.
When you run the entity sub-generator for an existing entity, you will
be asked a question ‘Do you want to update the entity? This will
replace the existing files for this entity, all your custom code will
be overwritten’ with following options:
Yes, re generate the entity - This will just regenerate your entity.
Tip: This can be forced by passing a --regenerate flag when running
the sub-generator
Yes, add more fields and relationships - This will
give you questions to add more fields and relationships
Yes, remove fields and relationships - This will give you questions to remove
existing fields and relationships from the entity
No, exit - This will exit the sub-generator without changing anything
You might want to
update your entity for the following reasons:
You want to add/remove fields and relationships to an existing entity
You want to reset your entity code to its original state
You have updated JHipster, and would like to have your entity generated with
the new templates
You have modified the .json configuration file (the
format is quite close to the questions asked by the generator, so it’s
not very complicated), so you can have a new version of your entity
You have copy/pasted the .json file, and want a new entity that is
very close to the copied entity

jhipster updating .jhipster/entities.json from java entities

When I need to update an entity in jhipster just run jhipster entity MyEntity and apply new changes as desired to both: the entity and the associated changelog. So far so good. But what if I want the inverse result: defining the new fields/relations in the entity class and propagate those changes through the changelog and the frontend entity?
In this case, for instance:
#Column(name = "name")
#NotNull
#Pattern(regexp = "[a-zA-Z0-9]")
private String name;
According to what I have read, If I already have the name field but I want to add the above validations I have to add them into the proper liquibase changelog first and then in my java entity? Is that the only way?
It would be great to use this workflow, however, by my understanding it is not possible.
A possible solution would be to first regenerate the entity using jhipster cli or any other jhipster method. Once this step is completed, you could edit the entity in Java, adding complex validations or even refining the entity's relationship.
After any entity modification you must update Liquibase's changelog. You could do it manually or you could run ./gradlew liquibaseDiffChangeLog to generate a changelog containing all changes applied over the database. Don't forget to apply the generated changelog to the main changelog (src/main/resources/liquibase/master.xml).
Cheers!

Unmanaged installation and Entities data

I have unmanaged solution installed in un online prod environnement and I want to install a new version of this solution in the same environnement, my question is that if I do that, what will happen to my data entities as I don't want to lose my data from the environnement?
Thanks in advance,
The data in the base tables will be not be altered when importing a new solution.
It will be the entity definition.
If a new attribute has been created for an entity which is set to Business Required then a null value will be held until a user opens the form which will require that value to be supplied before the record can be updated.
+1 to Stefan's answer. I will also add that unmanaged solutions will never delete any data. It is always additive, so if you remove an attribute from an entity in your solution in dev, it will not be removed when you import it into another environment. You'll need to manually track these removals and remove them post solution deployment.

How to change the display field to use when use jdl?

When use jhipster entity generator by console a question like follow allows to change the display field on frontend:
When you display this relationship with AngularJS, which field from
'user' do you want to use? (id)
It is possible to make the same with jdl?
Edited
Ok, It appears that nobody has this problem, or maybe there aren't anybody in home?
So, I'm going to explain it. Let's suppose this scenario:
Create two (or more) entities with relationship with the console.
Choose a different field from id to manage the front-end.
Test the project (it works fine).
Export the model to jdl file
Import the jdl file, with no changes, in another new project.
Boom! The UI comes back to Id reference.
Is there any option to resolve it, or is this resolve in other tools like uml import? Future plans?
Resolved
Reading the documentation in detail, I've found a solution:
"Declare the field you want to use to display a relationship in AngularJS.
To do that you must add the field name between () after the injected field name."
Resolved
Reading the documentation in detail, I've found a solution:
"Declare the field you want to use to display a relationship in
AngularJS.
To do that you must add the field name between () after the injected
field name."

Resources