Skip to content

Commit

Permalink
document that records can now be used as @IdClasses and @EmbeddableIds
Browse files Browse the repository at this point in the history
Signed-off-by: Gavin King <[email protected]>
  • Loading branch information
gavinking committed Apr 26, 2024
1 parent 647f689 commit a869ce1
Showing 1 changed file with 5 additions and 51 deletions.
56 changes: 5 additions & 51 deletions documentation/src/main/asciidoc/introduction/Entities.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -364,64 +364,23 @@ class Book {
But this approach comes with a problem: what object can we use to identify a `Book` and pass to methods like `find()` which accept an identifier?

The solution is to write a separate class with fields that match the identifier attributes of the entity.
The `@IdClass` annotation of the `Book` entity identifies the id class to use for that entity:
Every such id class must override `equals()` and `hashCode()`.
Of course, the easiest way to satisfy these requirements is to declare the id class as a `record`.

[source,java]
----
class BookId {
String isbn;
int printing;
BookId() {}
BookId(String isbn, int printing) {
this.isbn = isbn;
this.printing = printing;
}
@Override
public boolean equals(Object other) {
if (other instanceof BookId) {
BookId bookId = (BookId) other;
return bookId.isbn.equals(isbn)
&& bookId.printing == printing;
}
else {
return false;
}
}
@Override
public int hashCode() {
return isbn.hashCode();
}
}
record BookId(String isbn, int printing) {}
----

Every id class should override `equals()` and `hashCode()`.
The `@IdClass` annotation of the `Book` entity identifies `BookId` as the id class to use for that entity.

This is not our preferred approach.
Instead, we recommend that the `BookId` class be declared as an `@Embeddable` type:

[source,java]
----
@Embeddable
class BookId {
String isbn;
int printing;
BookId() {}
BookId(String isbn, int printing) {
this.isbn = isbn;
this.printing = printing;
}
...
}
record BookId(String isbn, int printing) {}
----

We'll learn more about <<embeddable-objects>> below.
Expand Down Expand Up @@ -881,11 +840,6 @@ record Name(String firstName, String middleName, String lastName) {}

In this case, the requirement for a constructor with no parameters is relaxed.

[NOTE]
====
Unfortunately, as of May 2023, Java `record` types still cannot be used as ``@EmbeddedId``s.
====

We may now use our `Name` class (or record) as the type of an entity attribute:

[source,java]
Expand Down

0 comments on commit a869ce1

Please sign in to comment.