0

我有一个带有外部生成密钥(从物理卡读取)的 JDO 卡实体。这链接到与 Card 实体同时创建的 User 实体。当我读取卡片并调用方法时,我希望 AppEngine 要么返回我之前创建的卡片和用户,要么创建一个新的。但是,如果我快速连续调用该方法两次,最终会覆盖 Card 实体并创建两个 User 身份。

这是我的代码(我已经删除了 getter 和 setter):

CardEntity.java

@PersistenceCapable public class CardEntity {

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

用户实体.java

@PersistenceCapable
public class CustomerEntity {

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

来自 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;
    }

我尝试了很多事情,但我经常最终创建了多个 UserEntities。上面的代码在一个实体组(由 rootkey 定义)中包含所有 CardEntities,我什至还对根实体进行了修改。这似乎没有帮助。

任何提示,指针?

4

1 回答 1

1

go for:getObjectById并捕获异常。

getObjectById 具有很强的一致性,这意味着,如果它在创建后立即存在,它应该立即返回一个对象。查询只有最终一致性,这意味着它可能在创建后找到,但也可能没有。

如果 not foundgetObjectById会抛出一个 Entity not found 异常,该异常可以单独捕获和处理。

于 2013-01-28T15:43:40.407 回答