jakartaee / jakartaee/persistence
Ease use of records of basic types
- Dominant language
- Java
- Stars
- 268
- Forks
- 78
- Avg merge
- 1d 6h
- Merged PRs (30d)
- 13
Description
JPA allows fields to be [Basic Types](https://jakarta.ee/specifications/persistence/3.2/jakarta-persistence-spec-3.2#a486) or [Embeddable Classes](https://jakarta.ee/specifications/persistence/3.2/jakarta-persistence-spec-3.2#a487), the latter of which can be a `record`. An existing pattern for type safety is to wrap a Java primitive types, `UUID`, or `String` (all of which are basic types) in a record. For example, instead of representing all the IDs as `long`s or `UUID`s and all names as `String`, one can use `record BookId(long id)`, `record CatalogId(long id)`, `record BookName(String name)`, and `record StreetName(String name)` as a means to enforce correctness.
Currently, this is possible through embeddable types:
```java
@Entity
public class Library {
@EmbeddedId
LibraryId id;
@Embedded
LibraryName name;
@Embedded
StreetName streetName;
@Embedded
CatalogId catalogId;
}
// no annotation needed according to @EmbeddedId's docs
public record LibraryId(UUID libraryId) {}
@Embeddable
public record StreetName(String streetName) {}
@Embeddable
public record LibraryName(String name) {}
@Embeddable
public record CatalogId(long catalogId) {}
```
The use of records in such a way will probably increase in popularity when [value classes](https://openjdk.org/jeps/401) are introduced, which will minimize the performance impact of the wrapper indirection.
I'd like to explore the idea of extending the [allowed persistent fields](https://jakarta.ee/specifications/persistence/3.2/jakarta-persistence-spec-3.2#a19) to include "records of Basic Types". This will eliminate the "noise"/ceremony of the `@Embeddable` <-> `@Embedded` annotations. In terms of implementation, there is no ambiguity - the field is created through the canonical constructor and retrieved through its accessor. This change is also backwards compatible. The risk here is increasing the complexity of the mental model of the user and the code as it may not be clear if a type requires the annotations or not. However, this is part of the user's domain and their responsibility, and the current list of allowed non-primitive types is already *relatively arbitrary* (I use this term loosely, it's various time-related classes).
Contributor guide
Research direction
Start with the Jakarta Persistence 3.2 section on allowed persistent fields and compare it with the linked Basic Types and Embeddable Classes sections. Review the record examples, including their canonical constructors and accessors; done means the proposed treatment of records wrapping Basic Types is resolved and reflected consistently in the persistence specification.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- database
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100