I'm still fairly new to working with java and the google appengine datastore.
I can put data in and get it out of the datastore, and I am trying to make it so that a user cannot be entered twice. As there is no unique index on the datastore, I'm setting a hash of the users email address as a primarykey.
Strangely, when I enter the same data multiple times, it is being entered into the datastore (which I thought would have returned an error, or done nothing).
So when I set my emailhash to '2' for testing, and then run the insert script a bunch of times, and the query WHERE _emailHash='2', I get 3 results.
Here is the class where I am defining the user.
@Entity public class user { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long _uid; @PrimaryKey private String _emailHash; private String _firstName; private String _lastName; private String _email; private String _password; public Long getUid() { return _uid; } public String getEmailHash(){ return _emailHash; } public void setEmailHash(String emailHash) { _emailHash = emailHash; } public String getFirstName() { return _firstName; } public void setFirstName(String firstName) { _firstName = firstName; } public String getLastName() { return _lastName; } public void setLastName(String lastName) { _lastName = lastName; } public String getEmail() { return _email; } public void setEmail(String email) { _email = email; } public String getPassword() { return _password; } public void setPassword(String password) { _password = password; } }
The google documentation says the following
an entity ID ("key name") provided by the application when the object is created. Use this for objects without entity group parents whose IDs should be provided by the application. The application sets this field to the desired ID prior to saving. import javax.jdo.annotations.PrimaryKey; // ... @PrimaryKey private String name;
at http://code.google.com/appengine/docs/java/datastore/creatinggettinganddeletingdata.html
Is there a better way to guarantee uniques? Or do I have to check if the value exists each time before inserting?
--------------------------UPDATE---------------------------------------------- As per Dmitri's response, I was mixing JPA and JDO (or at least getting confused between the two). Now that I've got that sorted out, my hashed email definition looks like this
@Id @Extension(vendorName="datanucleus", key="gae.encoded-pk", value="true") private String _emailHash; @GeneratedValue(strategy = GenerationType.IDENTITY) private Long _uid;
Unfortunately when trying to create a user with
$pm = EMF::createEntityManager(); $user = new user(); $user->setEmailHash('5'); $user->setFirstName('test5'); $user->setLastName('test5'); $user->setEmail('test5'); $pm->persist($user);
I get the following error
Invalid primary key for com.nextweeq.scheduler.user. The primary key field is an encoded String but an unencoded value has been provided
So far my searches are returning something about specifying the keyname, but I haven't quite found the solution yet.