Create model and CRUD with JHipster terminal generator


Hey, today we will focus on generating application model. JHipster gives us an opportunity to create it veeery fast.  Generates all needed class files and view in angular (or other framework which you will choose).

During creating model with JHipster you are choosing how should looks like your entity. You need to type entity name, field name, types, restrictions (minValue, maxValue, etc), relations to other entities, pagination if needed, generating DTO and service class for your logic. Bunch of files at the beginning, isn’t it? So what way are available for creating all those files?

Spis treści

Methods of generating model in JHipster

1. Using command line

Generator is asking all questions one by one. It uses default values when you press enter. You need to remember that if you have any relation in your main entity, you need to generate the second entity before you invoke it in main one. Seems logic, generator needs to know the second one 😉

If you pass the whole generation process, it will list you all added files. Then you need to rebuild project and new structures will be available!

C:\hip\eventsearch>yo jhipster:entity prelegent

The entity prelegent is being created.


Generating field #1

? Do you want to add a field to your entity? Yes
? What is the name of your field? name
? What is the type of your field? String
? Do you want to add validation rules to your field? Yes
? Which validation rules do you want to add? Required

================= Prelegent =================
Fields
name (String) required 


Generating field #2

? Do you want to add a field to your entity? Yes
? What is the name of your field? bio
? What is the type of your field? String
? Do you want to add validation rules to your field? No

================= Prelegent =================
Fields
name (String) required 
bio (String)


Generating field #3

? Do you want to add a field to your entity? Yes
? What is the name of your field? websiteUrl
? What is the type of your field? String
? Do you want to add validation rules to your field? No

================= Prelegent =================
Fields
name (String) required 
bio (String)
websiteUrl (String)


Generating field #4

? Do you want to add a field to your entity? No

================= Prelegent =================
Fields
name (String) required 
bio (String)
websiteUrl (String)


Generating relationships to other entities

? Do you want to add a relationship to another entity? No

================= Prelegent =================
Fields
name (String) required 
bio (String)
websiteUrl (String)



? Do you want to use a Data Transfer Object (DTO)? No, use the entity directly
? Do you want to use separate service class for your business logic? No, the REST controller should use the repository directly
? Do you want pagination on your entity? Yes, with pagination links

Everything is configured, generating the entity...

   create .jhipster\Prelegent.json
   create src\main\resources\config\liquibase\changelog\20170322233100_added_entity_Prelegent.xml
   create src\main\java\com\pdb\eventsearch\domain\Prelegent.java
   create src\main\java\com\pdb\eventsearch\repository\PrelegentRepository.java
   create src\main\java\com\pdb\eventsearch\web\rest\PrelegentResource.java
   create src\main\java\com\pdb\eventsearch\repository\search\PrelegentSearchRepository.java
   create src\test\java\com\pdb\eventsearch\web\rest\PrelegentResourceIntTest.java
   create src\test\gatling\simulations\PrelegentGatlingTest.scala
 conflict src\main\resources\config\liquibase\master.xml
? Overwrite src\main\resources\config\liquibase\master.xml? overwrite
    force src\main\resources\config\liquibase\master.xml
 conflict src\main\java\com\pdb\eventsearch\config\CacheConfiguration.java
? Overwrite src\main\java\com\pdb\eventsearch\config\CacheConfiguration.java? overwrite
    force src\main\java\com\pdb\eventsearch\config\CacheConfiguration.java
   create src\main\webapp\app\entities\prelegent\prelegents.html
   create src\main\webapp\app\entities\prelegent\prelegent-detail.html
   create src\main\webapp\app\entities\prelegent\prelegent-dialog.html
   create src\main\webapp\app\entities\prelegent\prelegent-delete-dialog.html
   create src\main\webapp\app\entities\prelegent\prelegent.state.js
   create src\main\webapp\app\entities\prelegent\prelegent.controller.js
   create src\main\webapp\app\entities\prelegent\prelegent-dialog.controller.js
   create src\main\webapp\app\entities\prelegent\prelegent-delete-dialog.controller.js
   create src\main\webapp\app\entities\prelegent\prelegent-detail.controller.js
   create src\main\webapp\app\entities\prelegent\prelegent.service.js
   create src\main\webapp\app\entities\prelegent\prelegent.search.service.js
   create src\test\javascript\spec\app\entities\prelegent\prelegent-detail.controller.spec.js
   create src\test\javascript\e2e\entities\prelegent.js
 conflict src\main\webapp\app\layouts\navbar\navbar.html
? Overwrite src\main\webapp\app\layouts\navbar\navbar.html? overwrite
    force src\main\webapp\app\layouts\navbar\navbar.html
   create src\main\webapp\i18n\en\prelegent.json
 conflict src\main\webapp\i18n\en\global.json
? Overwrite src\main\webapp\i18n\en\global.json? overwrite
    force src\main\webapp\i18n\en\global.json
   create src\main\webapp\i18n\pl\prelegent.json
 conflict src\main\webapp\i18n\pl\global.json
? Overwrite src\main\webapp\i18n\pl\global.json? overwrite
    force src\main\webapp\i18n\pl\global.json

Running `gulp inject` to add JavaScript to index.html

[00:31:10] Using gulpfile C:\hip\eventsearch\gulpfile.js
[00:31:10] Starting 'inject:app'...
[00:31:10] gulp-inject 115 files into index.html.
[00:31:10] Finished 'inject:app' after 215 ms

2. Using free online tool

You can buidl you ERD model in JDL studio. What you type will be visualised in the scheme on the right. You can define there all informations, including relations, paginations, and many more. Then you need to export JDL file and read it via command line using tutorial from documentation.

3. Using UML schema

You can generate model from diagram using JHipster-UML which supports many formats.

 

Genertion of files lasts very quickly and the results looks like the one below:

On .jhipster folder in the root folder you will find json files corresponding to entities which was created by you. In that json files you can modify field names, relations or other configurations. After that you need to regenerate entity using our favorited command:

yo jhipster:entity prelegent

{
    "fluentMethods": true,
    "relationships": [],
    "fields": [
        {
            "fieldName": "name",
            "fieldType": "String",
            "fieldValidateRules": [
                "required"
            ]
        },
        {
            "fieldName": "bio",
            "fieldType": "String"
        },
        {
            "fieldName": "websiteUrl",
            "fieldType": "String"
        }
    ],
    "changelogDate": "20170322233100",
    "dto": "no",
    "service": "no",
    "entityTableName": "prelegent",
    "pagination": "pagination"
}

I need to spend some time on preparing model for this project. Maybe this weekend will be more generous than the one before 🙂

Paweł Dobrzański

Start-up's fan, technological conferences member, social media enthusiast and low-cost trips traveler.

You may also like

LEAVE A COMMENT

Cześć!

Witaj na moim blogu! Znajdziesz tu proces budowania mojej aplikacji EventSearch za pomocą jhipstera. Podzielę się z Tobą niuansami dot. zakładania i prowadzenia firmy. Mam również nadzieję, że pomogę Ci zaoszczędzić trochę pieniędzy. Zapraszam!

Najpopularniejsze posty

Dzięki, że wpadłeś!

Paweł Dobrzański

Paweł Dobrzański

Start-up's fan, technological conferences member, social media enthusiast and low-cost trips traveler.