npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

generator-jhipster-optimistic-lock

v0.1.0

Published

A JHipster blueprint that adds JPA optimistic locking (@Version) to SQL entities.

Readme

generator-jhipster-optimistic-lock

JHipster blueprint that adds JPA optimistic locking (@Version) to the generated SQL entities

NPM version Generator Samples

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 User entity, embedded entities and entities with an existing version field 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-lock

Usage

jhipster-optimistic-lock

or, with the global JHipster CLI:

jhipster --blueprints optimistic-lock

Selecting 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 0 on the existing rows. It works with and without JHipster's incrementalChangelog.

  • 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 @OptimisticLock later stops generating the changelog and the @Version field, but does not drop the column: add a dropColumn changeset if needed.

Development

npm install
npm test

Generate 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 verify

Pre-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