Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You're having two separate problems here. Let's take the easy one first.</p> <hr> <p><strong>javax.ejb.EJBTransactionRolledbackException: Entity not managed</strong></p> <p>The <code>List</code> of objects returned by that query is not <em>itself</em> an <code>Entity</code>, and so you can't <code>.refresh</code> it. In fact, that's what the exception is complaining about. You're asking the <code>EntityManager</code> to do something with an object that is simply not a known <code>Entity</code>.</p> <p>If you want to <code>.refresh</code> a bunch of things, iterate through them and <code>.refresh</code> them individually.</p> <hr> <p><strong>Refreshing the list of ItemStatus</strong></p> <p>You're interacting with Hibernate's <code>Session</code>-level cache in a way that, from your question, you don't expect. From the <a href="http://docs.jboss.org/hibernate/orm/4.1/manual/en-US/html_single/#transactions-basics-identity" rel="noreferrer">Hibernate docs</a>:</p> <blockquote> <p>For objects attached to a particular Session (i.e., in the scope of a Session)... JVM identity for database identity is guaranteed by Hibernate.</p> </blockquote> <p>The impact of this on <code>Query.getResultList()</code> is that you do not necessarily get back the most up to date state of the database. </p> <p>The <code>Query</code> you run is really getting a list of entity IDs that match that query. Any IDs that are already present in the <code>Session</code> cache are matched up to known entities, while any IDs that are not are populated based on database state. The previously known entities <em>are not</em> refreshed from the database at all.</p> <p>What this means is that in the case where, between two executions of the <code>Query</code> from within the same transaction, some data has changed in the database for a particular known entity, the second Query would <strong>not</strong> pick up that change. It would, however, pick up a brand new <code>ItemStatus</code> instance (unless you were using a <a href="http://docs.jboss.org/hibernate/orm/4.1/manual/en-US/html_single/#performance-querycache" rel="noreferrer">query cache</a>, which I assume you're not).</p> <p>Long story short: With Hibernate, whenever you want to, within a single transaction, load an entity and then pick up additional changes to that entity from the database, you must explicitly <code>.refresh(entity)</code>.</p> <p>How you want to deal with this depends a bit on your use case. Two options I can think of off the bat: </p> <ol> <li>Have are to have the DAO tied to the lifespan of the transaction, and lazily initialize the <code>List&lt;ItemStatus&gt;</code>. Subsequent calls to <code>DAO.refreshList</code> iterate through the <code>List</code> and <code>.refresh(status)</code>. If you also need newly added entities, you should run the <code>Query</code> and <em>also</em> refresh the known ItemStatus objects.</li> <li>Start a new transaction. Sounds like from your chat with @Perception though that that is not an option. </li> </ol> <hr> <p><strong>Some additional notes</strong></p> <p>There was discussion about using query hints. Here's why they didn't work:</p> <p><em>org.hibernate.cacheable=false</em> This would only be relevant if you were using a <a href="http://docs.jboss.org/hibernate/orm/4.1/manual/en-US/html_single/#performance-querycache" rel="noreferrer">query cache</a>, which is only recommended in very particular circumstances. Even if you were using it though, it wouldn't affect your situation because the query cache contains object IDs, not data.</p> <p><em>org.hibernate.cacheMode=REFRESH</em> This is a directive to Hibernate's <a href="http://docs.jboss.org/hibernate/orm/4.1/manual/en-US/html_single/#performance-cache" rel="noreferrer">second-level cache</a>. If the second-level cache were turned on, AND you were issuing the two queries from different transactions, then you would have gotten stale data in the second query, and this directive would have resolved the problem. But if you're in the same Session in the two queries, the second level cache would only come in to play to avoid database loading for entities that are new to this <code>Session</code>.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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