tinmegali
5/10/2018 - 6:59 PM

JHipster Entity Update

Proceedings to updated entities using jhipster

Official Workflow

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 ./mvnw liquibase:diff (or ./mvnw 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 -PrunList=diffLog, and change the database configuration in build.gradle in the liquibase configuration if required.

Creating/Editing an Entity

There are 2 main options to create/edit an entity. It is possible to use jhipster's entity manager or create/edit and import an jhioster .jdl using JDLStudio.

JDL Studio

  • Create/Edit the entity
  • Download the jdl file
  • import the file on terminal, paying attention to the selected options. important: Don't override liquibase files if you´re plannning in migrate your database. If you're on early stages of development, fell free to override all files.
jhipster import-jdl {filename}
  • The following proceedings will depend if you want to maintain the db or start with a clean one

JHipser Entity Creation

Follow JHipster inscrutions about how to use the tool.

Proceedings after an entity is updated

Once the editing process is completed, it is neecessary to address the database changes on liquibase. It is important to notice that jhipster doesn't manage this kind of database change automatically.

You must decide if it is necessary to migrate the database or if it may be dropped.

Maintaining the DB

To migrate the database, it is necessary to create a liquibase changelog, to inform liquibase about database changes, making the appropriate modification, like inserting standard values in certain fields.

  • on terminal, call ./gradlew liquibaseDiffChangeLog
  • a file with a new changelog will be created in src\main\resources\config\liquibase\changelog
  • the file must be added to master.xml, located on src\main\resources\config\liquibase\
  • add the file at the base of the master.xml

Dropping the database

Considering that the database was changed using some of the jhipster system, call on terminal

  • ./gradlew clean to wipe out the data
  • ./gradlew liquibaseClearChecksums to clear liquibase checksums, validating the changed changelogs

Adding to cache

When using cache, it may be necessary to update CacheConfiguration.java, including new entities and its foreign keys. Notice the @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE) annotation in the entity class.

@ApiModel(description = "Pesquisa completa")
@Entity
@Table(name = "pesquisa")
\!h // using cache for entity
\!h @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Pesquisa implements Serializable {

  @OneToMany(mappedBy = "pesquisa")
  @JsonIgnore
\!h  // using cache for foreign key
\!h  @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
  private Set<AgeGroup> ageGroup = new HashSet<>();
}

The cache configuration must be updated.

@Configuration
@EnableCaching
@AutoConfigureAfter(value = { MetricsConfiguration.class })
@AutoConfigureBefore(value = { WebConfigurer.class, DatabaseConfiguration.class })
public class CacheConfiguration {
  // ...
  @Bean
    public JCacheManagerCustomizer cacheManagerCustomizer() {
        return cm -> {
          // ...
          \!h cm.createCache(br.com.pixinside.arcelor.linhadotempo.futuro.domain.Pesquisa.class.getName(), jcacheConfiguration);
          \!h cm.createCache(br.com.pixinside.arcelor.linhadotempo.futuro.domain.Pesquisa.class.getName() + ".ageGroup", jcacheConfiguration);
          // ...
        }
}