Module jakarta.data

Annotation Interface By


@Retention(RUNTIME) @Target(PARAMETER) public @interface By

Annotates a parameter of a repository method, specifying a mapping to an entity attribute:

  • if an attribute name is specified, the parameter maps to the entity attribute with the specified name, or
  • if the special value "id(this)" is specified, the parameter maps to the unique identifier attribute.

Arguments to the annotated parameter are compared to values of the mapped attribute. The equality comparison is default. Use the @Is annotation to choose a different subtype of Constraint to be the comparison.

The attribute name may be a compound name like address.city.

For example, for a Person entity with attributes ssn, firstName, lastName, and address we might have:


 @Repository
 public interface People {

     @Find
     Person findById(@By(ID) String id); // maps to Person.ssn

     @Find
     List<Person> findNamed(@By("firstName") String first,
                            @By("lastName") String last);

     @Find
     List<Person> findByCity(@By("address.city") String city);
 }
 

The By annotation is unnecessary when the method parameter name matches the entity attribute name and the application is compiled with the -parameters compiler option that makes parameter names available at runtime.

Thus, when this compiler option is enabled, the previous example may be written without the use of By:


 @Repository
 public interface People {

     @Find
     Person findById(String ssn);

     @Find
     List<Person> findNamed(String firstName,
                            String lastname);

     @Find
     List<Person> findByCity(String address_city);
 }
 
  • Required Element Summary

    Required Elements
    Modifier and Type
    Required Element
    Description
    The name of the entity attribute mapped by the annotated parameter, or "id(this)" to indicate the unique identifier attribute of the entity.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
    The special value which indicates the unique identifier attribute.
  • Field Details

    • ID

      static final String ID

      The special value which indicates the unique identifier attribute. The annotation By(ID) maps a parameter to the identifier.

      Note that id(this) is the expression in JPQL for the unique identifier of an entity with an implicit identification variable.

      See Also:
  • Element Details

    • value

      String value
      The name of the entity attribute mapped by the annotated parameter, or "id(this)" to indicate the unique identifier attribute of the entity.
      Returns:
      the entity attribute name, or "id(this)" to indicate the unique identifier attribute.