Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Short answer. Change <code>RecipeJDO.setIngredients()</code> to this:</p> <pre><code>public void setIngredients(List&lt;IngredientJDO&gt; ingredients) { this.ingredients.clear(); this.ingredients.addAll(ingredients); } </code></pre> <p>When you fetch the RecipeJDO, the <code>ingredients</code> list is not a real <code>ArrayList</code>, it is a dynamic proxy that handles the persistence of the contained elements. You shouldn't replace it.</p> <p>While the persistence manager is open, you can iterate through the <code>ingredients</code> list, add items or remove items, and the changes will be persisted when the persistence manager is closed (or the transaction is committed, if you are in a transaction). Here's how you would do the update without a transaction:</p> <pre><code>public void updateRecipe(String id, List&lt;IngredientDTO&gt; newIngredients) { List&lt;IngredientJDO&gt; ingredients = convertIngredientDtosToJdos(newIngredients); PersistenceManager pm = PMF.get().getPersistenceManager(); try { RecipeJDO recipe = pm.getObjectById(RecipeJDO.class, id); recipe.setIngredients(ingredients); } finally { pm.close(); } } </code></pre> <p>If you never modify the <code>IngredientJDO</code> objects (only replace them and read them), you might want to make them <code>Serializable</code> objects instead of JDO objects. If you do that, you may be able to reuse the <code>Ingredient</code> class in your GWT RPC code.</p> <p>Incidentally, even if <code>Recipe</code> was not a JDO object, you would want to make a copy in the <code>setIngredients()</code> method, otherwise someone could do this:</p> <pre><code>List&lt;IngredientJDO&gt; ingredients = new ArrayList&lt;IngredientJDO&gt;; // add items to ingredients recipe.setIngredients(ingredients); ingredients.clear(); // Woops! Modifies Recipe! </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload