google app engine - keys and unique rows in appengine datastore -


i'm still new working java , google appengine datastore.

i can put data in , out of datastore, , trying make user cannot entered twice. there no unique index on datastore, i'm setting hash of users email address primarykey.

strangely, when enter same data multiple times, being entered datastore (which thought have returned error, or done nothing).

so when set emailhash '2' testing, , run insert script bunch of times, , query _emailhash='2', 3 results.

here class defining 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 following

 entity id ("key name") provided application when object created. use objects without entity group parents ids should provided application. application sets field desired id prior saving. import javax.jdo.annotations.primarykey;  // ...     @primarykey     private string name; 

at http://code.google.com/appengine/docs/java/datastore/creatinggettinganddeletingdata.html

is there better way guarantee uniques? or have check if value exists each time before inserting?

--------------------------update---------------------------------------------- per dmitri's response, mixing jpa , jdo (or @ least getting confused between two). i've got sorted out, hashed email definition looks

 @id  @extension(vendorname="datanucleus", key="gae.encoded-pk", value="true")  private string _emailhash;   @generatedvalue(strategy = generationtype.identity)  private long _uid; 

unfortunately when trying create user

 $pm = emf::createentitymanager();  $user = new user();  $user->setemailhash('5');  $user->setfirstname('test5');  $user->setlastname('test5');  $user->setemail('test5');   $pm->persist($user); 

i following error

 invalid primary key com.nextweeq.scheduler.user. primary key field encoded string unencoded value has been provided 

so far searches returning specifying keyname, haven't quite found solution yet.

documentation referring jdo, while code seems using jpa @id plays same role @primarykey plays in jdo


Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -