Primary Key
A primary key uniquely identifies a document in the collection and enforces the unique constraint. In the absence of a user-defined primary key, it is auto-generated.
Defining a Primary Key
The example below demonstrates how primary key is defined. The
number in @TigrisPrimaryKey(order = 1)
represents the order of the field
in the primary key. While autoGenerate = true
represents that the values for
this field will be automatically generated by Tigris.
public class User implements TigrisDocumentCollectionType {
@TigrisPrimaryKey(order = 1, autoGenerate = true)
private int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
User user = (User) o;
return id == user.id;
}
@Override
public int hashCode() {
return Objects.hash(id);
}
}
Composite Primary Key
Composite primary keys are also supported but in case of composite keys order of the fields is important. The example below demonstrates how the order of the fields are defined in case of a composite primary key
public class User implements TigrisDocumentCollectionType {
@TigrisPrimaryKey(order = 1, autoGenerate = true)
private int id;
@TigrisPrimaryKey(order = 2)
private String email;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
User user = (User) o;
return id == user.id && age == user.age && Objects.equals(email, user.email);
}
@Override
public int hashCode() {
return Objects.hash(id, email, age);
}
}
Auto-generated Primary Key
If no primary key is specified in the model, Tigris automatically adds a
special _id
field of type UUID
as the primary key.
You can bind this auto-generated field to your Java model by adding a
field with name _id
and type java.util.UUID
as shown below.
public class Order implements TigrisDocumentCollectionType {
private UUID _id; // <-- this
private long buyerId;
private long sellerId;
private double tradePrice;
public UUID get_id() {
return _id;
}
public void set_id(UUID _id) {
this._id = _id;
}
public long getBuyerId() {
return buyerId;
}
public void setBuyerId(long buyerId) {
this.buyerId = buyerId;
}
public long getSellerId() {
return sellerId;
}
public void setSellerId(long sellerId) {
this.sellerId = sellerId;
}
public double getTradePrice() {
return tradePrice;
}
public void setTradePrice(double tradePrice) {
this.tradePrice = tradePrice;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Order order = (Order) o;
return buyerId == order.buyerId
&& sellerId == order.sellerId
&& Double.compare(order.tradePrice, tradePrice) == 0;
}
@Override
public int hashCode() {
return Objects.hash(buyerId, sellerId, tradePrice);
}
}
Alternatively, if you want to use a different field name in your Java model for
this auto-generated primary key, this is also possible. In the following example
the field named orderId
will be mapped to auto generated field _id
.
public class OrderCustomized implements TigrisDocumentCollectionType {
private long buyerId;
private long sellerId;
private double tradePrice;
@TigrisField(autoGeneratedFieldMapping = AutoGeneratedFields._ID) // <-- this
private UUID orderId;
public UUID getOrderId() {
return orderId;
}
public void setOrderId(UUID orderId) {
this.orderId = orderId;
}
public long getBuyerId() {
return buyerId;
}
public void setBuyerId(long buyerId) {
this.buyerId = buyerId;
}
public long getSellerId() {
return sellerId;
}
public void setSellerId(long sellerId) {
this.sellerId = sellerId;
}
public double getTradePrice() {
return tradePrice;
}
public void setTradePrice(double tradePrice) {
this.tradePrice = tradePrice;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
OrderCustomized that = (OrderCustomized) o;
return buyerId == that.buyerId
&& sellerId == that.sellerId
&& Double.compare(that.tradePrice, tradePrice) == 0
&& Objects.equals(orderId, that.orderId);
}
@Override
public int hashCode() {
return Objects.hash(buyerId, sellerId, tradePrice, orderId);
}
}