generator-jhipster-optimistic-lock
v0.1.0
Published
A JHipster blueprint that adds JPA optimistic locking (@Version) to SQL entities.
Maintainers
Readme
generator-jhipster-optimistic-lock
JHipster blueprint that adds JPA optimistic locking (
@Version) to the generated SQL entities
Introduction
This is a JHipster blueprint. For the selected entities it generates:
| Layer | Change |
| ----------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Entity | @Version @Column(name = "version", nullable = false) private Integer version; with getter and setter |
| Liquibase | dedicated changelog <date>_added_entity_lock_<Entity>.xml adding the version column (integer, not null, default 0) |
| DTO / MapStruct | version property in the DTO, mapped by MapStruct by name in both directions |
| REST PUT | version required: 400 Bad Request (error.versionnull) when missing, 409 Conflict (error.concurrencyFailure) when it does not match the stored one |
| REST PATCH | version optional: when sent and stale, 409 Conflict (error.concurrencyFailure) |
| Service/resource | saveAndFlush on update, so the response contains the incremented version |
| Angular | version in the entity model and kept in the update form, so it is sent back on save |
| i18n | error.versionnull translation in global.json |
| Integration tests | the generated <Entity>ResourceIT deletes the inserted entity by id (its version is stale after an update) |
The 409 Conflict response is produced by JHipster's own ExceptionTranslator, which maps any ConcurrencyFailureException
(Hibernate's ObjectOptimisticLockingFailureException included) to 409 with the error.concurrencyFailure message, already translated
in every JHipster language:
{
"type": "https://www.jhipster.tech/problem/problem-with-message",
"title": "Conflict",
"status": 409,
"message": "error.concurrencyFailure"
}Compatibility with other blueprints
The blueprint does not replace JHipster templates. The field is added through a fragment
(_persistClass_.java.jhi.optimistic_lock, sections classAdditionalFieldsSection and classAdditionalFieldsMethodsSection) merged into
JHipster's entity root template, and the other changes edit the generated files. It can be combined with blueprints that replace the entity, DTO or
mapper templates as long as they keep JHipster's fragment sections.
Supported applications
- SQL databases with Spring Data JPA (imperative). Reactive (R2DBC) applications, MongoDB, Cassandra, Couchbase and Neo4j are ignored.
- The built-in
Userentity, embedded entities and entities with an existingversionfield are skipped (with a warning for the latter). - Client: the server side works with every client; Angular needs the model/form change applied by the blueprint. React and Vue already send back every property of the loaded entity.
Prerequisites
As this is a JHipster blueprint, we expect you have JHipster basic knowledge:
Compatibility
| Blueprint | JHipster | Node.js | Java | | --------- | -------- | ------------------------ | ---- | | 0.x | 9.4.0 | ^22.18.0 || >= 24.11.0 | 21 |
Installation
npm install -g generator-jhipster-optimistic-lockUsage
jhipster-optimistic-lockor, with the global JHipster CLI:
jhipster --blueprints optimistic-lockSelecting the entities
By default optimistic locking is opt-in: only entities annotated with @OptimisticLock in JDL are changed.
@OptimisticLock
entity Invoice {
number String required
}
entity Country {
name String required
}The annotation is stored in .jhipster/Invoice.json:
{
"annotations": {
"optimisticLock": true
}
}To lock every SQL entity, enable optimisticLockAllEntities; @OptimisticLock(false) then excludes an entity.
| Option | CLI | JDL config | Default |
| ---------------------- | -------------------------------- | -------------------------------- | ------- |
| Lock every SQL entity | --optimistic-lock-all-entities | optimisticLockAllEntities true | false |
| Per entity (opt-in) | - | @OptimisticLock | - |
| Per entity (exclusion) | - | @OptimisticLock(false) | - |
The option is saved in .yo-rc.json ("optimisticLockAllEntities": true), so regenerations keep it.
application {
config {
baseName store
blueprints [generator-jhipster-optimistic-lock]
optimisticLockAllEntities true
}
entities *
}
@OptimisticLock(false)
entity AuditEntry {
message String
}REST clients
Send back the version received from the server:
PUT /api/invoices/1001
{ "id": 1001, "number": "INV-1", "version": 3 }- same version as the database:
200 OK, the response contains"version": 4; - older version (somebody else updated the entity):
409 Conflict, reload the entity and retry; - no version:
400 Bad Request(error.versionnull).
PATCH checks the version only when the request contains it.
Liquibase changelog
The column is not added to JHipster's <changelogDate>_added_entity_<Entity>.xml changeset. Every locked entity gets its own changelog,
included in the incremental section of master.xml (after the changelogs creating the tables and their constraints):
<!-- src/main/resources/config/liquibase/changelog/20261202013850_added_entity_lock_Invoice.xml -->
<changeSet id="20261202013850-optimistic-lock-invoice" author="jhipster">
<preConditions onFail="MARK_RAN">
<not>
<columnExists tableName="invoice" columnName="version" />
</not>
</preConditions>
<addColumn tableName="invoice">
<column name="version" type="integer" defaultValueNumeric="0">
<constraints nullable="false" />
</column>
</addColumn>
</changeSet>New entities: the table is created by JHipster's changelog, then the column is added by this one.
Existing entities and databases: JHipster's changeset is untouched, so its checksum stays valid; the new changeset adds the column and sets
0on the existing rows. It works with and without JHipster'sincrementalChangelog.The date is generated once and saved in the entity configuration (
.jhipster/Invoice.json), so regenerations rewrite the same file:{ "annotations": { "optimisticLock": true, "optimisticLockChangelogDate": "20261202013850" } }Removing
@OptimisticLocklater stops generating the changelog and the@Versionfield, but does not drop the column: add adropColumnchangeset if needed.
Development
npm install
npm testGenerate and build a sample (see RELEASE.md):
mkdir -p /tmp/optimistic-lock-sample && cd /tmp/optimistic-lock-sample
<path-to-blueprint>/cli/cli.cjs generate-sample sample-maven-app --skip-jhipster-dependencies --skip-install
./mvnw -ntp -P-webapp verifyPre-release
To use an unreleased version, install it using git.
npm install -g ldaloia-dev/generator-jhipster-optimistic-lock#main
jhipster --blueprints optimistic-lock --skip-jhipster-dependencies