If I have three model classes – Taxpayer, Criminal and Patient – can I use socialSecurityNumber as the Key ID for all three or do I need to make it unique even across model classes.
In other words, should it be like this:
String socialSecurityNumber = "123-45-6789";
Long now = new Long(System.currentTimeMillis()); // use time to create uniqueness
String name = now.toString() + "-" + socialSecurityNumber;
Key key = KeyFactory.createKey(getClass().getSimpleName(), name);
or this:
String socialSecurityNumber = "123-45-6789";
Key key = KeyFactory.createKey(getClass().getSimpleName(), socialSecurityNumber);
Yes. The key name forms only part of the key, along with the app ID, entity kind, and any ancestor keys, so it’s fine to use the same key name in different models.
Be aware, however, that a social security number makes a poor primary key for several reasons: