Skip to content

Identifier Annotation

Sai Pullabhotla edited this page Oct 2, 2017 · 1 revision

The Identifier annotation is used to denote the ID/Key field of an entity. Identifiers uniquely identify a given entity within the Cloud Datastore. Every entity must have one and only one field marked with @Identifier annotation. The ID field may be declared anywhere in the Entity hierarchy; i.e., either in the Entity class itself or one of the super classes.

Valid Types for Identifiers

The field annotated with @Identifier must be one of the following types:

  • long (primitive)
  • java.lang.Long (wrapper)
  • java.lang.String
  • Any other object that meets the below criteria:
    • Has a public getValue method with no parameters and a return type of long, Long or String.
    • Has a public constructor with a single parameter whose type is same as the return type of getValue method.

Automatic ID Generation

Catatumbo supports automatic generation of IDs when inserting entities into the Cloud Datastore. Automatic generation can be turned off by specifying autoGenerated=false on the @Identifier annotation.

If the effective type of Identifier is long or Long, the ID generation is handled by the Google Cloud Datastore. For String type, Catatumbo generates the IDs using a random UUID.

When autoGenerated is set to false, the application is responsible for managing the identifiers.

Examples

Identifier with primitive long

In this example, an ID generated if the id is 0 at the time of insertion.

@Entity
public class Person {
  @Identifier
  private long id;

Identifier with wrapper long

In this example, an ID generated if the id is null or 0 at the time of insertion.

@Entity
public class Person {
  @Identifier
  private Long id;

Identifier with String type

In this example, an ID is not automatically generated. Application must set the ID to a non-null value at the time of insertion.

@Entity
public class User {
  @Identifier(autoGenerated=false)
  private String email;

Identifier with a Custom type

PersonId.java

Note that the PersonId class has a public constructor with a single argument of type long. There is also a getValue method with a return type of long.

public class PersonId {

  private long id;

  public PersonId(long id) {
    this.id = id;
  }

  public long getValue() {
    return id;
  }

}

Person.java

The example below uses a custom type, PersonId, as the identifier.

import java.time.LocalDate;

import com.jmethods.catatumbo.Entity;
import com.jmethods.catatumbo.Identifier;

@Entity
public class Person {
  @Identifier
  private PersonId id;
  private String name;
  private LocalDate birthDate;

  public PersonId getId() {
    return id;
  }

  public void setId(PersonId id) {
    this.id = id;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public LocalDate getBirthDate() {
    return birthDate;
  }

  public void setBirthDate(LocalDate birthDate) {
    this.birthDate = birthDate;
  }

}