Skip to content

Commit 671eb5a

Browse files
committed
readme
1 parent 79b05f3 commit 671eb5a

File tree

1 file changed

+30
-5
lines changed

1 file changed

+30
-5
lines changed

README.md

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Contributions for other ORMs or alternative JPA implementations are welcome.
1212

1313
For seamless type support in Hibernate ORM, you should pick one of the following variants:
1414

15-
| Hibernate Version | Artefact |
15+
| Hibernate Version | Artifact |
1616
|-------------------------------|----------------------------------------------------------------------------------------------------------------------|
1717
| 6.6, 6.5, 6.4, and 6.3 | [org.framefork:typed-ids-hibernate-63](https://central.sonatype.com/artifact/org.framefork/typed-ids-hibernate-63) |
1818
| 6.2 | TBD |
@@ -39,6 +39,35 @@ In some cases, that means changing the default DDL that Hibernate uses for the `
3939
The library only sets the type if there is no JDBC type for `SqlTypes.UUID` already set,
4040
which means that if you want to use something different you should be able to do so using a custom `org.hibernate.boot.model.TypeContributor`.
4141

42+
## UUID as an application-generated identifier
43+
44+
One of the goals of this library is to enable generated typed IDs _in application code_ - specifically in entity constructors.
45+
Being able to generate identifiers in app code solves many problems around application design and architecture by getting rid of the dependency of entity on the database.
46+
The classic approach is to let the database generate the identifiers, which is perfectly fine if you prefer that, but it breaks entity state because until you persist them they're invalid and incomplete.
47+
But when you generate the ID at construction time, the entity is valid from the first moment.
48+
49+
The only way to do this reliably is to generate random identifiers so that you don't get conflicts when persisting the entities, but using perfectly random values has its problems...
50+
51+
## UUID as a primary key
52+
53+
The `ObjectUuid.randomUUID()` generates [UUIDv7](https://www.toomanyafterthoughts.com/uuids-are-bad-for-database-index-performance-uuid7/#uuid-7-time-ordered) instead of Java's default UUIDv4.
54+
The UUIDv4 is not well suited to be used in indexes and primary keys due to performance reasons.
55+
The UUIDv7, while still larger than plain `long`, does not suffer from the performance problems that UUIDv4 has, and can be safely used for primary keys.
56+
57+
In case you don't like the default generator, you can opt to replace it using `ObjectUuid.Generators.setFactory(UuidGenerator.Factory)`.
58+
It might come in handy in tests where you might want to use a deterministic generator.
59+
60+
Some additional resources:
61+
62+
* [UUID vs Bigint Battle! - Scaling Postgres 302](https://www.scalingpostgres.com/episodes/302-uuid-vs-bigint-battle/)
63+
* [Illustrating Primary Key models in InnoDB and their impact on disk usage - Percona Blog](https://www.percona.com/blog/illustrating-primary-key-models-in-innodb-and-their-impact-on-disk-usage/)
64+
* [GUID/UUID Performance Breakthrough - Rick James](https://mysql.rjweb.org/doc.php/uuid)
65+
* [MySQL InnoDB Primary Key Choice: GUID/UUID vs Integer Insert Performance - KCCoder](https://kccoder.com/mysql/uuid-vs-int-insert-performance/)
66+
* [Unreasonable Defaults: Primary Key as Clustering Key - Use The Index, Luke](https://use-the-index-luke.com/blog/2014-01/unreasonable-defaults-primary-key-clustering-key)
67+
* [SQL server full-text index and its stop words - Jiangong Sun](https://jiangong-sun.medium.com/sql-server-full-text-index-and-its-stop-words-492b0b589bff)
68+
* [MySQL UUIDs – Bad For Performance - Percona Blog](https://www.percona.com/blog/uuids-are-popular-but-bad-for-performance-lets-discuss/)
69+
* [Choose the right primary key to save a large amount of disk I/O - Too Many Afterthoughts](https://www.toomanyafterthoughts.com/primary-key-random-sequential-performance/)
70+
4271
## Usage: ObjectUUID
4372

4473
The base type is designed to wrap a native UUID, and allows you to expose any utility functions you may need.
@@ -96,10 +125,6 @@ data class User(id: Id) {
96125
}
97126
```
98127

99-
The `ObjectUuid.randomUUID()` generates [UUIDv7](https://www.toomanyafterthoughts.com/uuids-are-bad-for-database-index-performance-uuid7/#uuid-7-time-ordered)
100-
using [com.fasterxml.uuid:java-uuid-generator](https://github.com/cowtowncoder/java-uuid-generator). If desired, this library could be improved to allow configuring the used generator,
101-
but given the goal of having typed PK IDs for tables/entities I doubt the need for anything other than UUIDv7.
102-
103128
## Usage: Enabling automatic type registration with Hibernate ORM
104129

105130
Install the [newest version](https://central.sonatype.com/artifact/org.atteo.classindex/classindex) of [org.atteo.classindex:classindex](https://github.com/atteo/classindex), and register it as an annotation processor.

0 commit comments

Comments
 (0)