【问题标题】:JDO Query returns zero resultsJDO 查询返回零结果
【发布时间】:2012-07-22 22:08:48
【问题描述】:

我正在构建一个 GWT 应用程序来访问在服务器端使用 JDO 持久保存的房屋列表。然后通过 RPC“getHouseService”将房屋列表移动到客户端以进行显示和操作。之后它们不会被返回并保存在服务器端。

当我运行应用程序并执行 RPC 服务时,客户端总是得到一个空列表。 在测试过程中,我注意到我的 ide (eclipse) 在调试模式下注意到 getHouse 中返回的列表的值是 StreamingQueryResult。

我做错了什么??我被这个问题困扰了一个多星期,我在网上找不到相关的解决方案。

这是我编写的用于处理数据持久性的代码:

public static PersistenceManagerFactory getPersistenceManagerFactory() {
        return pmfInstance;
    }

    public void addHouse(List<House> listofHouses) {
        PersistenceManager pm = getPersistenceManagerFactory().getPersistenceManager(); 
        try {
            pm.makePersistentAll(listofHouses);
            System.out.println("after makePersistentAll");
        }
        finally {
            pm.close();
        }
    }

    @SuppressWarnings("unchecked")
    public List<House> getHouses() {
        PersistenceManager pm = getPersistenceManagerFactory().getPersistenceManager();
    List<House> houseList = (List<House>) pm.newQuery("select from " + House.class.getName()).execute();
    return houseList;
    }

House 类是:

package vancouverHEA.shared;

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;

import org.datanucleus.jpa.annotations.Extension;

import com.google.gwt.user.client.rpc.IsSerializable;

@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable = "true")
public class House extends Building implements IsSerializable {

    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    @Extension(vendorName = "datanucleus", key = "gae.encoded-pk", value = "true")
    private Long key;
    @Persistent (serialized = "true")
    java.lang.Double landPrice;
    @Persistent (serialized = "true")
    java.lang.Double housePrice;
    @Persistent (serialized = "true")
    java.lang.Boolean forSale;
    @Persistent (serialized = "true")
    private java.lang.String realtorContact;
    @Persistent (serialized = "true")
    private java.lang.Integer listPrice;
    @Persistent (serialized = "true")
    private java.lang.Integer yrBuilt;
    @Persistent (serialized = "true")
    private java.lang.Integer yrReno;
    @Persistent (serialized = "true")
    private java.lang.String postalCode;
    @Persistent (serialized = "true")
    java.lang.Double latitude;
    @Persistent (serialized = "true")
    java.lang.Double longitude;

    String STRATA = "Strata";
    String SINGLE = "Single House" ;


    public House(double landPrice, double improvPrice, String address,
            String postalCode, int yrBuilt, int yrReno) {
        super();
        this.landPrice = landPrice;
        this.housePrice = improvPrice;
        this.address = address;
        this.postalCode = postalCode.trim().toUpperCase();
        this.yrBuilt = yrBuilt;
        this.yrReno = yrReno;
    }


    public String getPostalCode() {
        return postalCode;
    }


    public void setPostalCode(String postalCode) {
        this.postalCode = postalCode.trim().toUpperCase();
    }


    public double getLatitude() {
        return latitude;
    }


    public void setLatitude(double latitude) {
        this.latitude = latitude;
    }


    public double getLongitude() {
        return longitude;
    }


    public void setLongitude(double longitude) {
        this.longitude = longitude;
    }



    public double getPrice() {
        return this.landPrice + this.housePrice;
    }

    public boolean isForSale() {
        return forSale;
    }


    public void setForSale(boolean forSale) {
        this.forSale = forSale;
    }


    public String getRealtorContact() {
        return realtorContact;
    }


    public void setRealtorContact(String realtorContact) {
        this.realtorContact = realtorContact;
    }


    public int getListPrice() {
        return listPrice;
    }


    public void setListPrice(int listPrice) {
        this.listPrice = listPrice;
    }


    public int getYrBuilt() {
        return yrBuilt;
    }


    public void setYrBuilt(int yrBuilt) {
        this.yrBuilt = yrBuilt;
    }


    public int getYrReno() {
        return yrReno;
    }


    public void setYrReno(int yrReno) {
        this.yrReno = yrReno;
    }


    public void setLandPrice(Double landPrice) {
        this.landPrice = landPrice;
    }

    public double getLandPrice(){
        return landPrice;
    }

    public void setHousePrice(Double housePrice) {
        this.housePrice = housePrice;
    }

    public double getHousePrice() {
        return housePrice;
    }

}

【问题讨论】:

  • 为什么要序列化 ​​Double、String、Boolean、Integer 类型的字段?因此它们在查询中不可用。显然日志会告诉你正在发生的一切,所以最好检查一下

标签: gwt jdo


【解决方案1】:

这可能与此问题中描述的问题相同:GWT retrieve list from datastore via serviceimpl 总而言之,列表是延迟加载的,因此如果您通过 RPC 返回列表,它尚未读取并返回一个空列表。问题https://stackoverflow.com/a/7646549/66416的答案建议在返回之前调用列表上的size(),这将触发列表被读取(虽然不确定这是否是最优雅的解决方案)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-08
    • 2013-04-29
    • 1970-01-01
    • 1970-01-01
    • 2014-10-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多