Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You have to refersh entity which was returned by <code>entityManager.merge()</code> method, something like:</p> <pre><code>@Override public void refreshCollection(List&lt;T&gt; entityCollection){ for(T entity: entityCollection){ if(!this.getEntityManager().contains(entity)){ this.getEntityManager().refresh(this.getEntityManager().merge(entity)); } } } </code></pre> <p>This way you should get rid of <code>javax.ejb.EJBTransactionRolledbackException: Entity not managed</code> exception.</p> <p><strong>UPDATE</strong></p> <p>Maybe it's safer to return new collection:</p> <pre><code>public List&lt;T&gt; refreshCollection(List&lt;T&gt; entityCollection) { List&lt;T&gt; result = new ArrayList&lt;T&gt;(); if (entityCollection != null &amp;&amp; !entityCollection.isEmpty()) { getEntityManager().getEntityManagerFactory().getCache().evict(entityCollection.get(0).getClass()); T mergedEntity; for (T entity : entityCollection) { mergedEntity = entityManager.merge(entity); getEntityManager().refresh(mergedEntity); result.add(mergedEntity); } } return result; } </code></pre> <p>or you can be more effective if you can access entity IDs like this:</p> <pre><code>public List&lt;T&gt; refreshCollection(List&lt;T&gt; entityCollection) { List&lt;T&gt; result = new ArrayList&lt;T&gt;(); T mergedEntity; for (T entity : entityCollection) { getEntityManager().getEntityManagerFactory().getCache().evict(entity.getClass(), entity.getId()); result.add(getEntityManager().find(entity.getClass(), entity.getId())); } return result; } </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