Using unencrypted key and real key, benefits?
I am reading the docs for generating keys in an application. I'm not sure what effect using a simple String key has a real key. For example, when my users register, they must provide a unique username:
class User {
/** Key type = unencoded string. */
@PrimaryKey
private String name;
}
now if I understand the docs correctly, I can still generate named keys and entity groups using this: ??
// Find an instance of this entity:
User user = pm.findObjectById(User.class, "myusername");
// Create a new obj and put it in same entity group:
Key key = new KeyFactory.Builder(
User.class.getSimpleName(), "myusername")
.addChild(Goat.class.getSimpleName(), "baa").getKey();
Goat goat = new Goat();
goat.setKey(key);
pm.makePersistent(goat);
the Goat instance should now be in the same entity group as this user, right? I mean there is no problem with leaving the primary key User as just a raw string?
Is there a performance advantage when using a key? Should I update to:
class User {
/** Key type = unencoded string. */
@PrimaryKey
private Key key;
}
// Generate like:
Key key = KeyFactory.createKey(
User.class.getSimpleName(),
"myusername");
user.setKey(key);
it's pretty much the same, i will still generate a key using a unique username anyway,
thanks
a source to share
When you specify a string key, like you are in your example, you are specifying the name of the key (see docs ). This way you don't have to use KeyFactory - just set the key field to "myusername".
There is no performance difference between the two parameters: internally they are stored the same way; the key name is just easier to use if you are not using parent objects for this model.
a source to share