I have just started with GAE+JPA. However I find it difficult to grasp the
primary key things.
I have created a very simple entity and a test to verify that the primary
key is generated automatically.
Entity
import java.util.Date;
import javax.persistence.Basic;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import com.google.appengine.api.datastore.Key;
import com.szczytowski.genericdao.api.IEntity;
@Entity
public class Profile implements IEntity<Key> {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Key _id;
@Basic
private int _age;
@Basic
@Temporal(TemporalType.DATE)
private Date _dob;
public Profile() {
}
public Key getId() {
return _id;
}
public void setId(Key id) {
_id = id;
}
public int getAge() {
return _age;
}
public void setAge(int age) {
_age = age;
}
public Date getDob() {
return _dob;
}
public void setDob(Date dob) {
_dob = dob;
}
}
Test Class
import java.util.Date;
import junit.framework.TestCase;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.ramanandi.matri.infrastructure.jpa.EMF;
import
com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig;
import
com.google.appengine.tools.development.testing.LocalServiceTestHelper;
public class ProfileDaoTest extends TestCase {
private final LocalServiceTestHelper helper = new
LocalServiceTestHelper(new LocalDatastoreServiceTestConfig());
private ProfileDao dao = new ProfileDao();
@Before
public void setUp() {
helper.setUp();
}
@After
public void tearDown() {
helper.tearDown();
}
private void doTest() {
dao.setEntityManager(EMF.get().createEntityManager());
Profile p = new Profile();
p.setAge(20);
p.setDob(new Date());
dao.save(p);
assertNotNull(p.getId());
}
@Test
public void testInsert1() {
doTest();
}
}
But assertNotNull(p.getId()); fails. Why the primary key is not generated
automatically? What's wrong.
Thanks
SN
--
You received this message because you are subscribed to the Google Groups
"Google App Engine for Java" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-appengine-java?hl=en.