0
votes

I am new to GAE and especially for Datastore (JDO)

I have an Object Composition : User object has a reference of Contact object. I am able to store them in datastore. But the code allows to store multiple objects with same “username” which is defined as primary key.

Here is the code snippet

//User class

@PersistenceCapable (identityType = IdentityType.APPLICATION)
public class User{

@PrimaryKey
@Persistent (valueStrategy = IdGeneratorStrategy.IDENTITY) 
String username;

@Persistent
Contact contact;
//getters and setters
}


// Contact  class

@PersistenceCapable (identityType = IdentityType.APPLICATION)
public class Contact {

@PrimaryKey    
@Persistent (valueStrategy = IdGeneratorStrategy.IDENTITY) 
private Key username; 

@Persistent
String Phone1;
}



//DAO class
public void register()  {
User user = new User();
user.setUserName("abc");

Contact contact=new Contact();
contact.setEmail("[email protected]");
user.setContact(contact);
pm.makePersistent(user);
}

If I call this register method twice (or equivalent to submit a registration form twice with same set of username and email id), the datastore is not complaining about duplicate key Exception.

Since I am creating "username" as my KEY I am expecting to get duplicate key Exception. But why is this not happening?

thanks Ma

1

1 Answers

0
votes

What "same set of username and email" ? You set username to be autogenerated, by JDO, so it generates the value for that field not you. Consequently it is unique. Consequently there is no exception.