Google App Engine encoded key in Java

Can anyone provide some simple code on how to use encoded key in Java for google app engine. The sample code is a bit confusing. Suppose I have an employee class and I want the primary key to be manually constructed with the format "name, email, phone", for example a sample key would be "James Smith, james @ mycompany.com, 12345678"

Regarding 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 encodedKey

and keyName

? After various attempts, I still get the following error:

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

+2


source to share


2 answers


You need to provide the name of the key, not the key. To do this, follow any of the suggestions in the error message, then provide your encoded string as the key name.



+2


source


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



+1


source







All Articles