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 9202535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:16:51+00:00 2026-06-17T23:16:51+00:00

I have a JDO Card entity with an externally generated Key (read from a

  • 0

I have a JDO Card entity with an externally generated Key (read from a physical card). This links to a User entity, which is created at the same time as the Card entity. When I read the card and call a method, I want AppEngine to either return me the Card and User previously created, or to create a new one. However, if I call the method twice in rapid succession, I end up with the Card entity being overwritten and two User identities being created.

Here’s my code (I’ve removed getters & setters):

CardEntity.java

@PersistenceCapable public class CardEntity {

    @PrimaryKey
    private Key Id;
    @Persistent
    @Unowned
    private UserEntity user;
}

UserEntity.java

@PersistenceCapable
public class CustomerEntity {

    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key key; 
    @Persistent 
    private String name;
}

method from Controller.java

    public UserEntity getOrCreateUser(String id) {
        CardEntity cardEntity;
        UserEntity userEntity;
        Query query;

        PersistenceManager mgr = getPersistenceManager();
        Transaction tx = mgr.currentTransaction();
        boolean complete = false;
        while (!complete) {
            try {
                tx.begin();

                query = mgr.newQuery(CardEntity.class);
                query.setUnique(true);
// rootkey is a key for a CardEntity that I am using as the parent for
// all CardEntities, so they are in the same enitity group, as a hack
// to try to solve this problem.
                Key key = KeyFactory.createKey(rootKey, "CardEntity", id);
                query.setFilter("id == :id");
                cardEntity = (CardEntity) query.execute(key);
                if (CardEntity == null) {
                    cardEntity = new CardEntity();
                    cardEntity.setId(key);
                    userEntity = new UserEntity();
                    mgr.makePersistent(userEntity);
                    nfcCardEntity.setUser(UserEntity);
                    mgr.makePersistent(cardEntity);
                    CardEntity rootCardEntity = 
                                        (CardEntity) mgr.getObjectById(CardEntity.class, rootKey);
                    rootCardEntity.setUser(userEntity);
// this is another hack to ensure that something changes in this transaction
// so that if there's a conflict, it will be rolled back
                    mgr.makePersistent(rootCardEntity);
                } else {
                    userEntity = cardEntity.getUser();
                }
                tx.commit();
            } finally {
                if (tx.isActive()) {
                    tx.rollback();
                } else {
                    complete = true;
                }
            }
        }
        mgr.close();
        return userEntity;
    }

I’ve tried numerous things, but I frequently end up with multiple UserEntities being created. The above code has all the CardEntities in one Entity Group (defined by rootkey), and I’ve even included a modification on the root Entity as well. This doesn’t seem to help.

Any tips, pointers?

  • 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-06-17T23:16:52+00:00Added an answer on June 17, 2026 at 11:16 pm

    go for: getObjectById and catch the exception.

    getObjectById have strong consitency wich means, it should immediately return an Object if it is there right after creation.
    Where the Query only have eventual consistency, which means it maybe found after creation but also maybe not.

    in case not found getObjectById throws a Entity not found exception which can catch and handle separately.

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

Sidebar

Related Questions

Hey guys, my question is about persisting an entity in JDO. I have created
I am using GAE(Java) with JDO for persistence. I have an entity with a
have written this little class, which generates a UUID every time an object of
I am working with AppEngine for Java, and I have a JDO entity defined
I have an application built with GWT/Appengine/Jdo...and i am using Google User Service for
I have two objects a user and a role which I make persistant using
I have a problem using GAEJ and JDO for storing the data. This is
I have a User object which has a collection of Transaction objects under it.
So I have saved a JDO entity that contains an value of MyEnum. public
I'm pretty new to JPA/JDO and the whole objectdb world. I have an entity

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.