Hi Rick, I got the reason. I always enhance entities by Eclipse plug-in. After got your clue, I enhanced entities again by using command. And the problem disappeared.
Really thank you for all the help. Alex, Huang ----- Original Message ----- From: "Rick Curtis" <[email protected]> To: <[email protected]> Sent: Friday, November 05, 2010 1:20 AM Subject: Re: Can't obtain Primary Key value by JPQL > How are you enhancing your Entities? If you are using build time, can you > post the enhanced .class file? > > Thanks, > Rick > > On Wed, Nov 3, 2010 at 8:32 PM, Alex, Huang <[email protected]> wrote: > >> Hi Rick, >> >> >How did you get the data into your DB? >> I insert data at mysql prompt. >> ================================================ >> mysql> INSERT INTO t_user(name,age) VALUES ('John', 26); >> Query OK, 1 row affected (0.03 sec) >> ================================================ >> >> Now,db's data like below, >> ================================================ >> mysql> select * from t_user; >> +-----+--------+-----+ >> | uid | name | age | >> +-----+--------+-----+ >> | 1 | Huang | 26 | >> | 2 | Alex | 28 | >> | 3 | tester | 22 | >> | 4 | John | 26 | >> +-----+--------+-----+ >> 4 rows in set (0.00 sec) >> ================================================ >> >> >What happens if you call em.find(TUser.class, [known_id]) ? >> >> I run the following case, and it completed successful. >> ================================================ >> public void testFind(){ >> EntityManager em = emf.createEntityManager(); >> em.getTransaction().begin(); >> TUser user = em.find(TUser.class, 2); >> assertEquals("Alex", user.getName()); >> assertEquals(28, user.getAge()); >> } >> ================================================ >> >> After I added "assertEquals(2,user.getUid());", AssertionFailedError >> occured. >> ================================================ >> junit.framework.AssertionFailedError: expected:<2> but was:<0> >> ================================================ >> >> >Could you try creating some data and persisting it via JPA? >> OK.I added a method to test case. >> ========================================== >> public void testInsert(){ >> EntityManager em = emf.createEntityManager(); >> em.getTransaction().begin(); >> TUser user = new TUser(); >> user.setName("Jeason"); >> user.setAge(27); >> em.persist(user); >> em.getTransaction().commit(); >> em.close(); >> } >> ========================================== >> >> I run the test case and it completed successful. At mysql prompt I >> confirmed >> the data has been created. >> >> ========================================== >> mysql> select * from t_user; >> +-----+--------+-----+ >> | uid | name | age | >> +-----+--------+-----+ >> | 1 | Huang | 26 | >> | 2 | Alex | 28 | >> | 3 | tester | 22 | >> | 4 | John | 26 | >> | 5 | Jeason | 27 | >> +-----+--------+-----+ >> 5 rows in set (0.00 sec) >> ========================================== >> >> Look forward to your advance. >> Alex,Huang >> ----- Original Message ----- >> From: "Rick Curtis" <[email protected]> >> To: <[email protected]> >> Sent: Thursday, November 04, 2010 5:29 AM >> Subject: Re: Can't obtain Primary Key value by JPQL >> >> >> > That looks about identical to the test case that I came up with. >> > >> >> Any advice? >> > I'm running low on ideas here... How did you get the data into your >> > DB? >> > What happens if you call em.find(TUser.class, [known_id]) ? Could you >> > try >> > creating some data and persisting it via JPA? >> > >> > Thanks, >> > Rick >> > >> >> >> >
