I have 2 classes which get saved in the google datastore. class product which contains an arraylist of productdetails. now i have a hard time getting the arraylist returned back to me.
class product is detachable and when i try to acces the arraylist inside the object i get the error:
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.
Can anyone explain how I can also detach the arraylist from the google datastore?
the 2 classes im using are
@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);
}
}
}
classe productdetails
@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.
}
the methode i’m using is in de DAO implementation to return the objects with there arraylist is.
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;
}
Why not just follow the advice of that message ? Put the field in the fetch group if you want to detach it. Docs for DataNucleus tell clearly enough how to do that