Module jakarta.data

Interface Attribute<T>

Type Parameters:
T - entity class of the static metamodel.
All Known Subinterfaces:
BasicAttribute<T,V>, ComparableAttribute<T,V>, NavigableAttribute<T,U>, NumericAttribute<T,N>, SortableAttribute<T>, TemporalAttribute<T,V>, TextAttribute<T>
All Known Implementing Classes:
AttributeRecord, SortableAttributeRecord, TextAttributeRecord

public interface Attribute<T>

Supertype for StaticMetamodel fields representing entity attributes.

The following subtypes are provided:

  • TextAttribute for entity attributes that represent text, typically of type String.
  • NumericAttribute for entity attributes that represent numeric values, such as int, Long, and BigDecimal.
  • TemporalAttribute for entity attributes that represent temporal values, such as LocalDateTime and Instant.
  • ComparableAttribute for entity attributes that represent other sortable and comparable values, such as boolean and enumerations.
  • SortableAttribute for entity types that are sortable, but incapable of order-based comparison. Generally this subtype is unused but is applicable for databases that allow sorting on attributes of type byte[].
  • NavigableAttribute for entity attributes that have attributes of their own. This is used for embeddables and associations to other entities.
  • BasicAttribute for other types of entity attributes, such as collections, embeddables, and other relation attributes.

To represent an entity, a static metamodel class defines a field corresponding to each entity attribute. The type of the field should be the most precise subtype of Attribute that describes the entity attribute type.

For example, given the following entity class,

 @Entity
 public class Car {
     @Id
     public String vin;
     public Color color;
     public double discountRate;
     public int firstModelYear;
     public LocalDate listed;
     public String make;
     public String model;
     public int price;
     public int year;
 }

 public enum Color { BLACK, BLUE, GRAY, RED, WHITE }
 

The static metamodel class (typically generated from the entity class) would be,

 @StaticMetamodel
 public interface _Car {
     String COLOR = "color";
     String DISCOUNTRATE = "discountRate";
     String FIRSTMODELYEAR = "firstModelYear";
     String LISTED = "listed";
     String MAKE = "make";
     String MODEL = "model";
     String PRICE = "price";
     String VIN = "vin";
     String YEAR = "year";

     ComparableAttribute<Car,Color> color = ComparableAttribute.of(
             Car.class, COLOR, Color.class);
     NumericAttribute<Car,Double> discountRate = NumericAttribute.of(
             Car.class, DISCOUNTRATE, double.class);
     NumericAttribute<Car,Integer> firstModelYear = NumericAttribute.of(
             Car.class, FIRSTMODELYEAR, int.class);
     TemporalAttribute<Car,LocalDate> listed = TemporalAttribute.of(
             Car.class, LISTED, LocalDate.class);
     TextAttribute<Car> make = TextAttribute.of(
             Car.class, MAKE);
     TextAttribute<Car> model = TextAttribute.of(
             Car.class, MODEL);
     NumericAttribute<Car,Integer> price = NumericAttribute.of(
             Car.class, PRICE, int.class);
     TextAttribute<Car> vin = TextAttribute.of(
             Car.class, VIN);
     NumericAttribute<Car,Integer> year = NumericAttribute.of(
             Car.class, YEAR, int.class);
 }
 
  • Method Summary

    Modifier and Type
    Method
    Description
    default Class<?>
    Obtain the Java class of the entity attribute.
    default Class<T>
    Obtain the Java class which declares this entity attribute.
    Obtain the entity attribute name, suitable for use wherever the specification requires an entity attribute name.
  • Method Details

    • name

      String name()
      Obtain the entity attribute name, suitable for use wherever the specification requires an entity attribute name. For example, as the parameter to Sort.asc(String).
      Returns:
      the entity attribute name.
    • declaringType

      default Class<T> declaringType()
      Obtain the Java class which declares this entity attribute.
      Returns:
      the declaring class
      Throws:
      UnsupportedOperationException - if the declaring type is not known.
      Since:
      1.1
      API Note:
      This is only guaranteed to be known if a static of method, such as BasicAttribute.of(Class, String, Class), was used to obtain the instance.
    • attributeType

      default Class<?> attributeType()
      Obtain the Java class of the entity attribute.
      Returns:
      the type of the entity attribute.
      Throws:
      UnsupportedOperationException - if the entity attribute type is not known.
      Since:
      1.1
      API Note:
      This is only guaranteed to be known if a static of method, such as BasicAttribute.of(Class, String, Class), was used to obtain the instance.