more use of typesafe metamodel gen refs

This commit is contained in:
Gavin 2023-06-10 23:08:49 +02:00 committed by Gavin King
parent fbb9df37bf
commit 95d3dfb85b
1 changed files with 14 additions and 11 deletions

View File

@ -53,7 +53,7 @@ Any entity or collection which is affected by a filter must be annotated `@Filte
[source,java] [source,java]
---- ----
@Entity @Entity
@Filter(name = "ByRegion") @Filter(name = example_.BY_REGION)
class User { class User {
@Id String username; @Id String username;
@ -64,13 +64,15 @@ class User {
} }
---- ----
Here, as usual, `example_.BY_REGION` is generated by the Metamodel Generator, and is just a constant with the value `"ByRegion"`.
If the `@Filter` annotation does not explicitly specify a restriction, the default restriction given by the `@FilterDef` will be applied to the entity. If the `@Filter` annotation does not explicitly specify a restriction, the default restriction given by the `@FilterDef` will be applied to the entity.
But an entity is free to override the default condition. But an entity is free to override the default condition.
[source,java] [source,java]
---- ----
@Entity @Entity
@Filter(name = "ByRegion", condition = "name = :region") @Filter(name = example_.FILTER_BY_REGION, condition = "name = :region")
class Region { class Region {
@Id String name; @Id String name;
@ -97,7 +99,7 @@ You should do this right at the _start_ of the session.
[source,java] [source,java]
---- ----
sessionFactory.inTransaction(session -> { sessionFactory.inTransaction(session -> {
session.enableFilter("ByRegion") session.enableFilter(example_.FILTER_BY_REGION)
.setParameter("region", "es") .setParameter("region", "es")
.validate(); .validate();
@ -812,11 +814,11 @@ class Book {
... ...
@ManyToOne(fetch = LAZY) @ManyToOne(fetch = LAZY)
@FetchProfileOverride(profile = "EagerBook", mode = JOIN) @FetchProfileOverride(profile = Book_.PROFILE_EAGER_BOOK, mode = JOIN)
Publisher publisher; Publisher publisher;
@ManyToMany @ManyToMany
@FetchProfileOverride(profile = "EagerBook", mode = JOIN) @FetchProfileOverride(profile = Book_.PROFILE_EAGER_BOOK, mode = JOIN)
Set<Author> authors; Set<Author> authors;
... ...
@ -829,13 +831,15 @@ class Author {
... ...
@OneToOne @OneToOne
@FetchProfileOverride(profile = "EagerBook", mode = JOIN) @FetchProfileOverride(profile = Book_.PROFILE_EAGER_BOOK, mode = JOIN)
Person person; Person person;
... ...
} }
---- ----
Here, once again, `Book_.PROFILE_EAGER_BOOK` is generated by the Metamodel Generator, and is just a constant with the value `"EagerBook"`.
For collections, we may even request subselect fetching: For collections, we may even request subselect fetching:
[source,java] [source,java]
@ -847,12 +851,13 @@ class Book {
... ...
@OneToOne @OneToOne
@FetchProfileOverride(profile = "EagerBook", mode = JOIN) @FetchProfileOverride(profile = Book_.PROFILE_EAGER_BOOK, mode = JOIN)
Person person; Person person;
@ManyToMany @ManyToMany
@FetchProfileOverride(profile = "EagerBook", mode = JOIN) @FetchProfileOverride(profile = Book_.PROFILE_EAGER_BOOK, mode = JOIN)
@FetchProfileOverride(profile = "BookWithAuthorsBySubselect", mode = SUBSELECT) @FetchProfileOverride(profile = Book_.BOOK_WITH_AUTHORS_BY_SUBSELECT,
mode = SUBSELECT)
Set<Author> authors; Set<Author> authors;
... ...
@ -879,8 +884,6 @@ session.enableFetchProfile(Book_.PROFILE_EAGER_BOOK);
Book eagerBook = session.find(Book.class, bookId); Book eagerBook = session.find(Book.class, bookId);
---- ----
Here, once again, `BookQueries_.PROFILE_EAGER_BOOK` is generated by the Metamodel Generator, and is just a constant with the value `"EagerBook"`.
So why or when might we prefer named fetch profiles to entity graphs? So why or when might we prefer named fetch profiles to entity graphs?
Well, it's really hard to say. Well, it's really hard to say.
It's nice that this feature _exists_, and if you love it, that's great. It's nice that this feature _exists_, and if you love it, that's great.