Larry Garfield
06/26/2024, 4:41 PMSzymon Jeziorski
06/26/2024, 8:26 PMval id: UUID = UUID.randomUUID()
and not worry about nullability no more. It should be random without any special strategies anyways, so I cannot think of any issues with it. That's also the approach I was successfully using in previous projects. And possibly before you ask, I would use nullable @Version
field for clear indication if entity was persisted (and also how many times it was updated)Larry Garfield
06/26/2024, 8:27 PMLarry Garfield
06/26/2024, 8:28 PMSzymon Jeziorski
06/26/2024, 8:31 PM@Version
val version: Long? = null
field will be null upon creation, but when persisting into the DB it will be automatically populated by Hibernate (and then incremented along each update)Larry Garfield
06/26/2024, 8:35 PMSzymon Jeziorski
06/26/2024, 8:38 PMSzymon Jeziorski
06/26/2024, 8:42 PM@Version
field could definitely come in handy considering how Spring Data JPA checks if the entity is new:
https://docs.spring.io/spring-data/jpa/reference/jpa/entity-persistence.html#jpa.entity-persistence.saving-entities.strategiesLarry Garfield
06/26/2024, 8:46 PMSzymon Jeziorski
06/26/2024, 8:52 PM@Version
field for some reason, you may use one of other strategies described there (if they're applicable in your case), for example implementing Persistable
. However, in my opinion adding @Version
is still worth it in most cases anyways, as it also allows Hibernate to handle optimistic locking mechanismLarry Garfield
06/26/2024, 8:55 PMLarry Garfield
06/26/2024, 8:56 PMSzymon Jeziorski
06/26/2024, 9:00 PMLarry Garfield
06/26/2024, 9:04 PMJasonB
06/26/2024, 9:10 PMJasonB
06/26/2024, 9:10 PMLarry Garfield
06/26/2024, 9:10 PMJasonB
06/26/2024, 9:12 PM@GeneratedValue(strategy = GenerationType.UUID)
JasonB
06/26/2024, 9:12 PMLarry Garfield
06/26/2024, 9:13 PM@Entity
@Table(name = "my_entity")
class MyEntity(
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(columnDefinition = "UUID NOT NULL default uuid_generate_v4()")
var id: UUID? = null,
)
(Though @Column is ignored, since we’re using Flyway with manual SQL files to actually setup the database. No idea why.)JasonB
06/26/2024, 9:16 PMUUID
instead of AUTO
and remove the nullLarry Garfield
06/26/2024, 9:16 PMJasonB
06/26/2024, 9:21 PMUUID.randomValue
although Hibernate might throw that awayJasonB
06/26/2024, 9:21 PMJasonB
06/26/2024, 9:21 PMLarry Garfield
06/26/2024, 9:23 PMJasonB
06/26/2024, 9:24 PMLarry Garfield
06/26/2024, 9:25 PMSzymon Jeziorski
06/27/2024, 6:03 AMid
out of constructor anyways. I would recommend this kind of simplified class structure:
@Entity
@Table(name = "table_name")
class EntityName(
[... class fields]
) {
@Id
val id: UUID = UUID.randomUUID()
@Version
val version: Int? = null
[...]
}
Since id
is assigned on the code level, there are no other annotations as @GeneratedValue
required (the value is already there, using @GeneratedValue
would tell Hibernate to possibly generate it again, and reassign it, which is wrong and also redundant), just @Id
is enough. What's obtained by UUID.randomUUID()
will then be inserted into the DB as primary key.
id
property being a val
declared and assigned within class body makes you sure that, firstly it cannot be reassigned after object instantiation, secondly it's always populated consistently (no risk of passing "wrong" value to the constructor), and lastly you save yourself from nullability hassle.
When you want to create new instance of an entity (hence new record in the DB), you would just invoke constructor without worrying about ID at all.
And when you read the row from the DB, Hibernate will instantiate appropriate object setting id
field to the value read from DB. Hibernate uses reflection for such mechanisms, so you don't have to make field re-assignable on the code level.
With this approach, you pretty much don't have to worry about `id`s, `version`s etc yourself, Hibernate does things for youJasonB
06/27/2024, 6:29 AMSzymon Jeziorski
06/27/2024, 6:31 AM@Version
useful here)Larry Garfield
06/27/2024, 1:18 PMJacob
06/30/2024, 9:52 PMSzymon Jeziorski
07/01/2024, 7:13 AMEntityInformation
within Spring Data Commons.
As a reference, here's one concrete implementation for JPA using @Version
attribute to resolve whether entity is new: https://github.com/spring-projects/spring-data-jpa/blob/018c833afd2219fb1598bae784[…]/data/jpa/repository/support/JpaMetamodelEntityInformation.java
And here's documentation reference listing resolve strategies: https://docs.spring.io/spring-data/jpa/reference/jpa/entity-persistence.html#jpa.entity-persistence.saving-entities.strategies
Thanks for the reply, but this was already discussed with here, with references to documentation. Please invest a minute to skim through the messages next time