如何在Google App Engine中比较非主键

问题描述 投票:0回答:1

如何在Google App engline中使用Java获取非主键列usedId的数据

List<UserAddress> list = (List<UserAddress>) pmf.getObjectById(UserAddress.class, Long.valueOf(userId));
        System.out.println(list.size());

当我获取数据时,在控制台中发生以下错误

NestedThrowablesStackTrace:
Could not retrieve entity of kind UserAddress with key UserAddress(4)
org.datanucleus.exceptions.NucleusObjectNotFoundException: Could not retrieve entity of kind UserAddress with key UserAddress(4)

也尝试在下面的代码二中获取非主键列userId的数据,但显示为empty list

@SuppressWarnings("unchecked")
public List<UserAddress> getUserAddressFind(String userId) {
    List<UserAddress> returnList = new ArrayList<UserAddress>();
    PersistenceManager pmf = PMF.get().getPersistenceManager();
    try {
        Query query = pmf.newQuery(UserAddress.class);
        query.setFilter("userId == userIdParam");
        query.declareParameters("Long userIdParam");
        returnList = (List<UserAddress>) query.execute(userId);
        System.out.println(returnList.size());
        if (returnList != null && returnList.isEmpty()) {
            System.out.println("No results for userAddresses");
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        pmf.close();
    }

    return returnList;
}

UserAddress.java

package com.rrd.up2me.datastore;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class UserAddress {

@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.SEQUENCE)
private Long userAddressId;

@Persistent
private Long userId;

@Persistent
private Long addressId;

@Persistent
private Boolean isPrimary;

public Long getUserAddressId() {
    return userAddressId;
}

public void setUserAddressId(Long userAddressId) {
    this.userAddressId = userAddressId;
}

public Long getUserId() {
    return userId;
}

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

public Long getAddressId() {
    return addressId;
}

public void setAddressId(Long addressId) {
    this.addressId = addressId;
}

public Boolean getIsPrimary() {
    return isPrimary;
}

public void setIsPrimary(Boolean isPrimary) {
    this.isPrimary = isPrimary;
}

}
java eclipse google-app-engine primary-key jdo
1个回答
0
投票

UserAddress.java]中]类中,当执行查询传递的变量类型为userId时,longString,因此未获取数据。在Type cast之后,将userId字符串转换为Long问题。

© www.soinside.com 2019 - 2024. All rights reserved.