Annotation Type Find

  • @Target(METHOD)
    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:

     class Book {
         @Id String isbn;
         String title;

    Then we might define:

     Book getBookForIsbn(String isbn);
     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.

    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, or Session, 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 @SQL.

    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:

    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 uses EntityManager.find(Class, Object) to retrieve the entity.
    • If the parameters match exactly with the @NaturalId field of the entity, the finder method uses Session.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, or Mutiny.Session.

    See Also:
    HQL, SQL
    • Element Detail

      • enabledFetchProfiles

        String[] enabledFetchProfiles