views:

361

answers:

2

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.

+1  A: 

Documentation you are referring to is about JDO, while your code seems to be using JPA where @Id plays the same role as @PrimaryKey plays in JDO

Dmitry
Thanks Dmitri, that may have been the first issue, but now I'm getting errors related to using the 'corresponding entity in the datastore' not having a name, which I'm not sure I completely understand.
pedalpete
A: 

You can use keys to retrive the data:

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

Key k = KeyFactory.createKey(user.class.getSimpleName(), "[email protected]");

PersistenceManager pm = PMF.get().getPersistenceManager();
user usr = pm.getObjectById(user.class, k);

Source: appengine docs

Kristian Damian
I'm not sure what you're getting at Kristian. How is using a key to retrieve data related to using data as a key? I am trying to set user data as a key, not retrieve data based on a key.
pedalpete
In the same way that's was retrival can be use as a key. Key k = KeyFactory.createKey(user.class.getSimpleName(), "[email protected]");Quoting google documentation:"Keys can be converted to and from a string representation using the KeyFactory class's keyToString() and stringToKey() methods, respectively. (Note that this is different from the Key class's toString() method, which returns a human-readable value suitable for debugging.) "
Kristian Damian