-
Notifications
You must be signed in to change notification settings - Fork 20
Identifier Annotation
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.
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 oflong
,Long
orString
. - Has a public constructor with a single parameter whose type is same as the return type of
getValue
method.
- Has a public
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.
In this example, an ID generated if the id is 0 at the time of insertion.
@Entity
public class Person {
@Identifier
private long id;
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;
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;
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;
}
}
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;
}
}