【问题标题】:JDO detach object and then get the arraylist of that objectJDO 分离对象,然后获取该对象的数组列表
【发布时间】:2011-12-14 22:14:55
【问题描述】:

我有 2 个类保存在 google 数据存储中。类产品,其中包含产品详细信息的数组列表。现在我很难让数组列表返回给我。 类产品是可拆卸的,当我尝试访问对象内的数组列表时出现错误:

You have just attempted to access field "productDetailsArray" yet this field was not detached when you detached the object. Either dont access this field, or detach it when detaching the object.

谁能解释我如何将数组列表从谷歌数据存储中分离出来?

我使用的两个类是

@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable = "true")
public class Product implements Serializable{
   private static final long serialVersionUID = 1L;
   private static final Logger log = Logger.getLogger(Product.class.getName());
   @PrimaryKey
   @Persistent
   private String productNaam;
   @Persistent
   private String categorie;
   @Persistent
   private ArrayList<ProductDetails> productDetailsArray = new ArrayList<ProductDetails>();

   //getters and setter below.

   // I use this methode to fix a error resulting the arraylist from being org.datanucleus.sco.backed.ArrayList
   public void fix() {
     log.info("Fix()");
     ArrayList<ProductDetails> a = new ArrayList<ProductDetails>();
     Iterator<ProductDetails> itr = productDetailsArray.iterator();
     while (itr.hasNext()) {
        ProductDetails p = itr.next();
        log.info(p.getPlaats());
        a.add(p);
     }
   }
}

分类产品详情

@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable = "true")
public class ProductDetails implements Serializable{
    private static final long serialVersionUID = 1L;
    @PrimaryKey    
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    @Extension(vendorName="datanucleus", key="gae.encoded-pk", value="true")
    private String encodedKey;  
    @Persistent
    private String winkel, plaats;
    @Persistent
    private double prijs;
    //getters and setters here.
}

我使用的方法是在 de DAO 实现中返回带有 arraylist 的对象。

public ArrayList<Product> productList() {
    PersistenceManager pm = PmfSingleton.get().getPersistenceManager();
    ArrayList<Product> products = new ArrayList<Product>();
    try {
        products.clear();
        pm.currentTransaction().begin();
        Extent<Product> ex = pm.getExtent(Product.class, true);
        Iterator<Product> itr = ex.iterator();
        while(itr.hasNext()) {
            Product tempProduct = itr.next();
            Product product = pm.detachCopy(tempProduct);
            //ArrayList<ProductDetails> pd = new ArrayList<ProductDetails>(product.getProductDetails());
            product.fix();
            products.add(product);
        }
        pm.currentTransaction().commit();
        ex.closeAll();
    } catch (Exception e) {
        pm.currentTransaction().rollback();
        throw new RuntimeException(e);
    } finally {
        pm.close();
    }
    return products;
}

【问题讨论】:

    标签: java gwt jdo gwt-rpc datanucleus


    【解决方案1】:

    为什么不听从那条信息的建议呢?如果要分离该字段,请将其放入 fetch 组中。 DataNucleus 的文档清楚地说明了如何做到这一点

    【讨论】:

    • 我正在使用 Spring 的 OpenEntityManagerInViewFilter,因此我不希望在针对同一个 Web 请求检索和访问该对象时分离该对象。 Eager 和 fetch 组是一种选择,但我很好奇为什么我需要这样做。我的猜测是,这是在我为另一个错误更改配置设置时引入的。
    • 我没有使用 ManyToOne 注释的字段。或者在我的情况下,对于 GAE 中的单亲限制,瞬态和单独的 Key 字段
    • 这与这个人的问题无关。如果你有问题,你可以用你自己的描述提出你自己的问题
    猜你喜欢
    • 1970-01-01
    • 2021-07-13
    • 2019-09-17
    • 2012-09-03
    • 2019-07-15
    • 2019-04-12
    • 1970-01-01
    • 2018-12-06
    • 1970-01-01
    相关资源
    最近更新 更多