views:

664

answers:

2

Can someone provide some simple code about how to use an encoded key in Java for the Google app engine. The sample code is a little bit confusing. Suppose I have an employee class and I want the primary key to be manually constructed with a format of "name, email, phone" for example a sample key would be"James Smith,[email protected],12345678"

With reference to the sample code

@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
@Extension(vendorName="datanucleus", key="gae.encoded-pk", value="true")
private String encodedKey;

@Persistent
@Extension(vendorName="datanucleus", key="gae.pk-name", value="true")
private String keyName;

What values if any would I assign to encodedKey and keyName? After various tries I'm still getting the following error:

The primary key field is an encoded String but an unencoded value has been provided. If you want to set an unencoded value on this field you can either change its type to be an unencoded String (remove the "gae.encoded-pk" extension), change its type to be a com.google.appengine.api.datastore.Key and then set the Key's name field, or create a separate String field for the name component of your primary key and add the "gae.pk-name" extension.

+1  A: 

You should be able to set a value for keyName, and let the gae api automatically populate encodedKey for you.

Peter Recore
+1  A: 

You need to set the key name, not the key. For that, follow any of the suggestions in the error message, then specify your encoded string as the key name.

Nick Johnson