Zankhna
Zankhna

Reputation: 4563

Primary key is not generated in JPA with app engine Java

I have entity class "User" as shown below but its not generating primary key. I am using JPA in my app engine application and using app engine endpoints in my android client.

Enitty class:

@Entity
public class UserMaster {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long userId;

    private String userName;
    private String fullName;
    private String userAvtarUrl;
    private String userAbout;
    private String userGender;


    public String getUserName() {
        return userName;
    }

    public Long getUserId() {
        return userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getFullName() {
        return fullName;
    }

    public void setFullName(String fullName) {
        this.fullName = fullName;
    }

    public String getUserAvtarUrl() {
        return userAvtarUrl;
    }

    public void setUserAvtarUrl(String userAvtarUrl) {
        this.userAvtarUrl = userAvtarUrl;
    }

    public String getUserAbout() {
        return userAbout;
    }

    public void setUserAbout(String userAbout) {
        this.userAbout = userAbout;
    }

    public String getUserGender() {
        return userGender;
    }

    public void setUserGender(String userGender) {
        this.userGender = userGender;
    }
}

Endpoint persistence code :

@ApiMethod(name = "insertUserMaster")
    public UserMaster insertUserMaster(UserMaster usermaster) {
        EntityManager mgr = getEntityManager();
        try {
            if (containsUserMaster(usermaster)) {
                throw new EntityExistsException("Object already exists");
            }
            mgr.persist(usermaster);
        } finally {
            mgr.close();
        }
        return usermaster;
    }

Android client

Userendpoint.Builder builder = new Userendpoint.Builder(
                        AndroidHttp.newCompatibleTransport(),
                        new JacksonFactory(), new HttpRequestInitializer() {
                            public void initialize(HttpRequest httpRequest) {
                            }
                        });

                Userendpoint endpoint = CloudEndpointUtils.updateBuilder(
                        builder).build();

                User objUser = new User();

                objUser.setUserName(txtName.getText().toString());
                objUser.setUserEmail(txtEmail.getText().toString());

                Bitmap bmp = BitmapFactory.decodeFile(imagePath);
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                bmp.compress(Bitmap.CompressFormat.PNG, 100, out);
                byte[] imgByte = out.toByteArray();
                String encodedImage = Base64.encodeToString(imgByte,
                        Base64.DEFAULT);
                objUser.setImage(encodedImage);

                User result = endpoint.insertUser(objUser).execute();

Please guide me where i am lacking. Thank you.

Upvotes: 0

Views: 118

Answers (3)

Kazimiras
Kazimiras

Reputation: 1

In my case problem was in method "containsUserMaster(usermaster)" Method check existing in way like this:

UserMaster item = mgr.find(UserMaster.class, userId);

Problem that until add operation not complete, userId will be null, and because program stops with NullPointerExeption.. I'm add userId checkinq on null - and this solves the problem

Upvotes: -1

DataNucleus
DataNucleus

Reputation: 15577

If using JPA and GAE/Datastore either use all JPA annotations, or all as a vendor-extension use all JDO annotations. You cannot mix and match.

Upvotes: 2

Zaw Than oo
Zaw Than oo

Reputation: 9935

May be your database server does not provide the auto id generation.

IDENTITY

  • IDENTITY, it is depend on the Database to auto generate.
  • Some DB Server Like, MySQL or Microsoft SQL Server do provide ID generation for the primary key field during insertion.

The common way is to use the IdGeneratorStrategy.TABLE. It is not depend on DB.

Upvotes: 0

Related Questions