gpt4 book ai didi

java - JDO分离对象,然后获取该对象的arraylist

转载 作者:行者123 更新时间:2023-11-30 04:56:04 24 4
gpt4 key购买 nike

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

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 实现中返回数组列表中的对象。

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

最佳答案

为什么不直接遵循该消息的建议呢?如果要分离该字段,请将其放入提取组中。 DataNucleus 的文档足够清楚地说明了如何做到这一点

关于java - JDO分离对象,然后获取该对象的arraylist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8512471/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com