Every JEHibernate entity extends one of three base classes. They differ only in the id type; all of them provide automatic timestamps, correct equals/hashCode, and an optimistic-locking version.
Pick a base class by id type
Base class
Id type
Use for
LongIdEntity
auto-increment Long
most entities (warps, homes, shops)
UuidEntity
generated UUID
players, distributed systems
StringIdEntity
custom String
natural keys (world names, permission nodes)
@Entity
@Table(name = "warps")
public class Warp extends LongIdEntity {
private String name;
protected Warp() {}
public Warp(String name) { this.name = name; }
public String getName() { return name; }
}
What you get for free
BaseEntity adds three managed fields to every entity:
createdAt — set once on insert (@CreationTimestamp, not updatable).
updatedAt — refreshed on every update (@UpdateTimestamp).
version — the optimistic-locking counter (@Version).
equals/hashCode are implemented by id, with a transient identity token so new (unsaved) entities are not all considered equal — they work correctly in a HashSet before they have an id.
UUID keys are dialect-portable
UuidEntity maps its id with the database's native UUID type — uuid on PostgreSQL and H2, binary(16) on MySQL/MariaDB, uniqueidentifier on SQL Server. You write UUID; JEHibernate picks the right column type per database.
Optimistic locking and the version column
The version field detects concurrent modifications: if two callers load the same row and both save, the second save fails instead of silently overwriting.
Adding @Version to an existing table
On Hibernate 7.2+ the version column is NOT NULL. You cannot add it to an already-populated table with ddl-auto=update — it is a migration step. See Migrations for the one-line ALTER.
getVersion() returns an int and is null-safe, so a legacy row from a pre-4.1 nullable column reads as 0 until you run the migration.
var warp = warpRepo.create(new Warp("spawn"));
warp.getCreatedAt(); // set automatically
warp.getUpdatedAt(); // set automatically
warp.getVersion(); // 0 on first persist
@Entity
public class PlayerData extends UuidEntity {
public PlayerData(UUID id) { setId(id); } // or let it auto-generate
}
var a = repo.findByIdOrThrow(id);
var b = repo.findByIdOrThrow(id);
a.setName("one");
repo.save(a); // version 0 -> 1
b.setName("two");
repo.save(b); // throws OptimisticLockException — b still held version 0