You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+26-19Lines changed: 26 additions & 19 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,8 +1,8 @@
1
1
# TypedIds
2
2
3
-
This library provides base classes and tooling to create typed IDs.
4
-
The original goal was to allow easy and safe creation of Value Objects (VOs) backed by UUIDv7 for Hibernate ORM entities.
5
-
Contributions for other ORMs or alternative JPA implementations are welcome.
3
+
This library provides base classes and tooling to easily and safely create immutable typed Value Object IDs (VO-IDs) internally backed by a UUIDv7 or bigint.
4
+
5
+
Contributions for serialization libraries, other ORMs, alternative JPA implementations or anything else are welcome.
6
6
7
7
## Why?
8
8
@@ -16,21 +16,10 @@ For seamless type support in Hibernate ORM, you should pick one of the following
Find the latest version in this project's GitHub releases or on Maven Central.
23
21
24
-
If you want just the plain classes, you can install only the [org.framefork:typed-ids](https://central.sonatype.com/artifact/org.framefork/typed-ids).
25
-
26
-
### Application-generated (random) IDs
27
-
28
-
This library supports several libraries for generating the IDs in the JVM but does not pull them in, instead it expects you to pick one and add it yourself.
29
-
30
-
* UUIDs with [`com.fasterxml.uuid:java-uuid-generator`](https://central.sonatype.com/artifact/com.fasterxml.uuid/java-uuid-generator/versions)
31
-
* BigInts/longs with [`io.hypersistence:hypersistence-tsid`](https://central.sonatype.com/artifact/io.hypersistence/hypersistence-tsid/versions)
32
-
33
-
If you want to use a different library, the `$Generators.setFactory()` extension point should hopefully be self-explanatory.
22
+
If you want just the plain base classes without ORM support, you can install just the [org.framefork:typed-ids](https://central.sonatype.com/artifact/org.framefork/typed-ids).
34
23
35
24
## Hibernate type mapping
36
25
@@ -48,14 +37,32 @@ In some cases, that means changing the default DDL that Hibernate uses for the `
48
37
The library only sets the type if there is no JDBC type for `SqlTypes.UUID` already set,
49
38
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`.
50
39
51
-
## UUID as an application-generated identifier
40
+
## Database-generated identifiers
41
+
42
+
The library explicitly supports `AUTO`, `IDENTITY` and `SEQUENCE` strategies for generating bigint identifiers via the `@GeneratedValue` annotation for the VO-IDs.
43
+
44
+
This pattern is supported only for `ObjectBigIntId`.
45
+
46
+
## Application-generated identifiers
47
+
48
+
One of the primary goals of this library is to enable generated typed IDs _in application code_ - specifically in entity constructors.
52
49
53
-
One of the goals of this library is to enable generated typed IDs _in application code_ - specifically in entity constructors.
54
50
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.
55
51
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.
56
52
But when you generate the ID at construction time, the entity is valid from the first moment.
53
+
The only way to do this reliably is to generate random identifiers so that you don't get conflicts when persisting the entities,
54
+
but using perfectly random values has its problems - see [UUID as a primary key](#uuid-as-a-primary-key) for a solution.
55
+
56
+
This pattern is supported for both `ObjectUuid` and `ObjectBigIntId`.
57
+
58
+
### Supported random application-generated identifiers generation strategies
59
+
60
+
This library supports several libraries for generating the IDs in the JVM but does not pull them in, instead it expects you to pick one and add it yourself.
57
61
58
-
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...
62
+
* UUIDs with [`com.fasterxml.uuid:java-uuid-generator`](https://central.sonatype.com/artifact/com.fasterxml.uuid/java-uuid-generator/versions)
63
+
* BigInts/longs with [`io.hypersistence:hypersistence-tsid`](https://central.sonatype.com/artifact/io.hypersistence/hypersistence-tsid/versions)
64
+
65
+
If you want to use a different library, the `$Generators.setFactory()` extension point should hopefully be self-explanatory.
59
66
60
67
## UUID as a primary key
61
68
@@ -77,7 +84,7 @@ Some additional resources:
77
84
*[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/)
78
85
*[UUID vs Bigint Battle! - Scaling Postgres 302](https://www.scalingpostgres.com/episodes/302-uuid-vs-bigint-battle/)
79
86
80
-
## Usage: ObjectUUID
87
+
## Usage: ObjectUuid
81
88
82
89
The base type is designed to wrap a native UUID, and allows you to expose any utility functions you may need.
83
90
The following snippet is the standard boilerplate, but you may opt to skip some of the methods, or add a few custom ones.
0 commit comments