OK.
=========================================================
package entity;
import java.io.Serializable;
import javax.persistence.*;
/**
* The persistent class for the t_user database table.
*
*/
@Entity
@Table(name="t_user")
public class TUser implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(insertable=false)
private int uid;
private int age;
private String name;
public TUser() {
}
public int getUid() {
return this.uid;
}
public void setUid(int uid) {
this.uid = uid;
}
public int getAge() {
return this.age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
}
Thanks.
Alex,Huang
=========================================================
----- Original Message -----
From: "Rick Curtis" <[email protected]>
To: <[email protected]>
Sent: Thursday, November 04, 2010 2:03 AM
Subject: Re: Can't obtain Primary Key value by JPQL
> Can you also post your entire Entity?
>
> Thanks,
> Rick
>
> On Wed, Nov 3, 2010 at 11:55 AM, Alex, Huang <[email protected]> wrote:
>
>> Hi Rick,
>>
>> Log:
>> ===================START==================================================
>> 188 JPATEST INFO [main] openjpa.Runtime - Starting OpenJPA 2.0.1
>> 3094 JPATEST TRACE [main] openjpa.jdbc.SQL - <t 30466761, conn
>> 14220570>
>> executing prepstmnt 23932998 SELECT t0.uid, t0.age, t0.name FROM t_user
>> t0
>> 3094 JPATEST TRACE [main] openjpa.jdbc.SQL - <t 30466761, conn
>> 14220570>
>> [0 ms] spent
>> ===================END====================================================
>>
>> Thanks in advance.
>> Alex,Huang
>>
>> ----- Original Message -----
>> From: "Rick Curtis" <[email protected]>
>> To: <[email protected]>
>> Sent: Wednesday, November 03, 2010 11:21 PM
>> Subject: Re: Can't obtain Primary Key value by JPQL
>>
>>
>> > Can you post the SQL trace from when you execute your query?
>> >
>> > Thanks,
>> > Rick
>> >
>> > On Wed, Nov 3, 2010 at 3:51 AM, Alex, Huang <[email protected]>
>> wrote:
>> >
>> >> Hi everybody,
>> >>
>> >> I am using openJPA2.0.1 + Mysql5.1.
>> >>
>> >> Table definition like below,
>> >> ===============DML START=======================
>> >> CREATE TABLE t_user (
>> >> uid int(10) unsigned NOT NULL AUTO_INCREMENT,
>> >> name varchar(255) DEFAULT NULL,
>> >> age int(11) NOT NULL,
>> >> PRIMARY KEY (uid)
>> >> ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
>> >> ===============DML END=========================
>> >>
>> >> entity,
>> >> ==========Entity START=========================
>> >> @Entity
>> >> @Table(name="t_user")
>> >> public class TUser implements Serializable {
>> >> private static final long serialVersionUID = 1L;
>> >>
>> >> @Id
>> >> @GeneratedValue(strategy=GenerationType.IDENTITY)
>> >> @Column(insertable=false)
>> >> private int uid;
>> >>
>> >> private int age;
>> >>
>> >> private String name;
>> >> ......getter and setter.....
>> >> ==========Entity END===========================
>> >>
>> >> ==========persistence.xml START===========================
>> >> ................
>> >> <class>entity.TUser</class>
>> >> <properties>
>> >> <property name="openjpa.jdbc.DBDictionary" value="mysql"/>
>> >> <property name="javax.persistence.jdbc.driver"
>> >> value="com.mysql.jdbc.Driver"/>
>> >> <property name="javax.persistence.jdbc.url"
>> >> value="jdbc:mysql://localhost:3306/test?characterEncoding=utf8"/>
>> >> <property name="javax.persistence.jdbc.user" value="root"/>
>> >> <property name="javax.persistence.jdbc.password" value="xxxxxx"/>
>> >> <property name="openjpa.Log" value="DefaultLevel=WARN, Runtime=INFO,
>> >> Tool=INFO, SQL=TRACE"/>
>> >> </properties>
>> >>
>> >> ==========persistence.xml END=============================
>> >>
>> >>
>> >> I run a JPQL like below,
>> >>
>> >> Query query = em.createQuery("SELECT user FROM TUSER user");
>> >>
>> >> For the result, I can get each field's value except primary key field.
>> In
>> >> this case, each entity's uid field is zero.
>> >>
>> >> I tried to trace the source code and I found the following code in
>> >> org.apache.openjpa.jdbc.kernel.JDBCStoreManager from LINE 1085.
>> >>
>> >> // load unloaded fields
>> >> FieldMapping[] fms = mapping.getDefinedFieldMappings();
>> >> Object eres, processed;
>> >> for (int i = 0; i < fms.length; i++) {
>> >> if (fms[i].isPrimaryKey() ||
>> >> sm.getLoaded().get(fms[i].getIndex()))
>> >> continue;
>> >>
>> >> It seemed that if the a field is primary key so no value would be
>> >> passed
>> >> to
>> >> it. Would someone tell me why? Did I miss any setting?
>> >>
>> >> Thanks and best regards,
>> >> Alex, Huang
>> >>
>> >>
>> >>
>> >
>>
>>
>>
>