0
votes

This question has already been asked. But my present situation is a bit different, and I am hoping there are now better answers. So here goes:

I need a datastore entity to have two unique keys, one being the primary key.

Will the following suffice? I need that when I make a keys only query for the datastore to understand that I am referring to keyA

@Entity
public class UserAccount implements Serializable{

  @Id
  @GeneratedValue(strategy=GenericType.IDENTITY)
  Key keyA;//based on email

  @PrimaryKey
  @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
  Key keyB;//randomly generated
  //….other data
}

I need the datastore to help enforce the uniqueness of the keys so it is not enough to let keyB be a simple field that I manage myself.

1
So does the combination of Key A and Key B also have to be unique? - Namphibian
incidentally, per my design. - learner
You already know your answer, the datastore has no built in support for SQL like "unique indexes". @Namphibian will probably tell you to concatenate both "ids" into a unique Key name. Managing yourself the uniqueness of the email property is actually good enough for your case, since you have zero risk of having two persons concurrently creating two UserAccounts with the same email... - Michael Técourt

1 Answers

0
votes

You'll have to enforce uniqueness yourself. That is on the non-primary key you'll have to do a look up and see if you get a result.