Yes, Dao Saves is properly. I just found that the Key will be generated on
EntityManager.flush()

I modified the code, and now it works fine.

    public void testPkGeneration() {

        Profile p1 = new Profile();
        p1.setAge(20);
        p1.setDob(new Date());

        EntityManager em = EMF.get().createEntityManager();
        EntityTransaction tx = em.getTransaction();
        dao.setEntityManager(em);
        tx.begin();
        dao.save(p1);
        em.flush();
        tx.commit();

        List<Profile> profiles = dao.getAll();
        assertNotNull(profiles.get(0));
        assertNotNull(p1.getId());

    }



On Wed, Apr 21, 2010 at 1:25 AM, Ikai L (Google) <[email protected]> wrote:

> Are you sure that the entity is being saved?
>
> Can you post your ProfileDAO?
>
> On Mon, Apr 19, 2010 at 9:52 AM, Sudhir Ramanandi <[email protected]>wrote:
>
>> 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]<google-appengine-java%[email protected]>
>> .
>> For more options, visit this group at
>> http://groups.google.com/group/google-appengine-java?hl=en.
>>
>
>
>
> --
> Ikai Lan
> Developer Relations, Google App Engine
> Twitter: http://twitter.com/ikai
> Delicious: http://delicious.com/ikailan
>
> ----------------
> Google App Engine links:
> Blog: http://googleappengine.blogspot.com
> Twitter: http://twitter.com/app_engine
> Reddit: http://www.reddit.com/r/appengine
>
>  --
> 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]<google-appengine-java%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/google-appengine-java?hl=en.
>

-- 
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.

Reply via email to