Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4013884
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:25:38+00:00 2026-05-20T09:25:38+00:00

When I persist an object to the datastore, when (and how) can I get

  • 0

When I persist an object to the datastore, when (and how) can I get the key of that particular object that I just persisted? So for example, if I have:

@PersistenceCapable
public class Employee {
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key id;
...
}

The Query class:

public class EmployeeQuery {
    // Persist a single Employee
    public void persistEmployee(Employee e) {
    // 1. Can I get the id at this point?
    PersistenceManager pm = PMF.get().getPersistenceManager();
    try {
        pm.makePersistent(e);
        // 2. Can I get the id at this point?
    } 
    finally {
        pm.close();
        // 3. Can I get the id at this point?
    }
    }
...
}

The PersistenceManager and PMF information can be found here: http://code.google.com/appengine/docs/java/datastore/jdo/overview.html#Getting_a_PersistenceManager_Instance

As mentioned above, where in the mentioned areas (1, 2 or 3) can I get the auto-generated id of that particular object? Also, how can I get the id of that specific object? Any suggestions of how to do this efficiently?

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T09:25:39+00:00Added an answer on May 20, 2026 at 9:25 am

    You can get the key in Point 2, once the object is persisted. Point 1 is too early and Point 3 is also called when an exception occurs, so you can not guarantee to have a generated key.

    As the official docs say : “The long key field of an instance is populated when the instance is saved.”

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Map, that I want to persist. The domain object is something
I have a Recipe entity that contains two images: @PersistenceCapable(identityType = IdentityType.APPLICATION, detachable=true) public
I need to persist an object that is not marked with the serializable attribute.
I have an application that has many different types of objects that each persist
Hibernate does not allow me to persist an object that contains an null embedded
Can I use Linq-to-xml to persist my object state without having to use/know Xpath
I'm having some trouble getting NH to persist my object graph. I have (something
I am getting below error whilst trying to persist an object that has a
I have a class called Parent , and I'm storing these object's in the
When I persist an object with a foreign key, I see in the logs

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.