Annotation Type Find
-
@Target(METHOD) @Retention(CLASS) @Incubating public @interface Find
Identifies a method of an abstract class or interface as defining the signature of a finder method, with an implementation generated automatically by the Hibernate Metamodel Generator.For example, suppose the entity
Book
is defined as follows:@Entity class Book { @Id String isbn; String title; ... }
Then we might define:
@Find Book getBookForIsbn(String isbn); @Find List<Book> getBooksWithTitle(String title);
Notice that:
- the types and names of the method parameters exactly match the types and names of the corresponding fields of the entity.
- there's no special naming convention for the
@Find
methods—they may be named arbitrarily, and their names encode no semantics.
It's even possible to query by a field of an embedded object:
@Find List<Book> publishedBooks(String publisher$name);
Here,publisher$name
refers to the fieldname
of theBook
'sPublisher
.The Metamodel Generator automatically creates an "implementation" of every finder method in the static metamodel class
Books_
. The generated method may be called according to the following protocol:Book book = Books_.findBookByIsbn(session, isbn); List<Book> books = Books_.getBooksWithTitle(session, String title);
Notice the extra parameter of type
EntityManager
at the start of the parameter list.Alternatively, the type to which the annotated method belongs may also declare an abstract method with no parameters which returns one of the types
EntityManager
,StatelessSession
, orSession
, for example:EntityManager entityManager();
In this case:- the generated method is no longer
static
, - the generated method will use this method to obtain the
session object, instead of having a parameter of type
EntityManager
, and - the generated static metamodel class will actually implement
the type which declares the method annotated
@Find
.
Thus, the generated method may be called according to the following protocol:
Books books = new Books_(session); Book book = books.getBookForIsbn(isbn); List<Book> books = books.getBooksWithTitle(String title);
This is reminiscent of traditional DAO-style repositories.
The return type of an annotated method must be an entity type
E
, or one of the following types:java.util.List<E>
,java.util.stream.Stream<E>
,java.util.Optional<E>
,io.smallrye.mutiny.Uni<E>
, when used with Hibernate Reactive,org.hibernate.query.Query<E>
,org.hibernate.query.SelectionQuery<E>
,jakarta.persistence.Query<E>
, orjakarta.persistence.TypedQuery<E>
.
The names and types of the parameters of a finder method must match exactly with the names and types of persistent fields of the entity type returned by the finder method.
- If there is one parameter, and it matches the
@Id
or@EmbeddedId
field of the entity, the finder method usesEntityManager.find(Class, Object)
to retrieve the entity. - Similarly, if there is one parameter, and its type matches the
type of the
IdClass
of the entity, the finder method usesEntityManager.find(Class, Object)
to retrieve the entity. In this case the parameter name is not significant. - If the parameters match exactly with the
@NaturalId
field or fields of the entity, the finder method usesSession.byNaturalId(Class)
to retrieve the entity. - Otherwise, the finder method builds and executes a criteria query.
As an exception, the method may have at most one parameter of type
EntityManager
,Session
,StatelessSession
, orMutiny.Session
. Furthermore, if the finder method returns multiple results, that is, if its return type isList
, then it may also have:- a parameter with type
Page
, and/or - a parameter with type
Order<? super E>
,List<Order<? super E>>
, orOrder<? super E>...
(varargs) whereE
is the entity type returned by the query.
For example:
@Find List<Book> getBooksWithTitle(String title, List<Order<Book>> order);
As a further exception, a method might support key-based pagination. Then it must have:
- return type
KeyedResultList
, and - a parameter of type
KeyedPage
.
-
-
Optional Element Summary
Optional Elements Modifier and Type Optional Element Description String[]
enabledFetchProfiles
-
-
-
Element Detail
-
enabledFetchProfiles
String[] enabledFetchProfiles
- Default:
- {}
-
-