Skip to content

annotation Athena::ORM::Annotations::ID #

Marks an instance variable as the entity's identifier, its primary key. Every entity must have an identifier, and the instance variable must also be mapped with AORMA::Column.

@[AORMA::Entity]
class User < AORM::Entity
  @[AORMA::Column]
  @[AORMA::ID]
  @[AORMA::GeneratedValue]
  property! id : Int64
end

Unless the database generates it, see AORMA::GeneratedValue, the identifier must be assigned before the entity is passed to AORM::EntityManager#persist.

Composite Keys#

Applying the annotation to more than one instance variable defines a composite primary key. Entities with one are found with a Hash holding a value for each identifier field:

@[AORMA::Entity]
class Translation < AORM::Entity
  @[AORMA::Column]
  @[AORMA::ID]
  property! article_id : Int64

  @[AORMA::Column]
  @[AORMA::ID]
  property! language : String

  @[AORMA::Column]
  property! title : String
end

em.find Translation, {"article_id" => 1, "language" => "en"}

On databases that support RETURNING, the database can also generate a composite key, in which case every identifier column is read back after the INSERT.

Todo

Using an association as part of the identifier isn't supported yet.