javaeclipsegoogle-app-engineprimary-keyjdo

How to join non primary key in google app engine


How to get data of non primary key column userId using java in google app engine

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

When I fetch the data the following error occur in console

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)

Tried below code also, to fetch the data of non primary key column userId but it shows 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;
}

}

Solution

  • In UserAddress.java class userId is long when execute the query passed variable type is Stringso data is not fetching. AfterType cast the userId String to Long problem solved.

    Ex: Long.valueOf(userId).

    Query query = pmf.newQuery(UserAddress.class);
    query.setFilter("userId == userIdParam");
    query.declareParameters("Long userIdParam");
    returnList = (List<UserAddress>) query.execute(Long.valueOf(userId));
    System.out.println(returnList.size());