Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to refresh JPA entities when backend database changes asynchronously?
    primarykey
    data
    text
    <p>I have a PostgreSQL 8.4 database with some tables and views which are essentially joins on some of the tables. I used NetBeans 7.2 (as described <a href="http://netbeans.org/kb/70/websvc/rest.html" rel="noreferrer">here</a>) to create REST based services derived from those views and tables and deployed those to a Glassfish 3.1.2.2 server.</p> <p>There is another process which asynchronously updates contents in some of tables used to build the views. I can directly query the views and tables and see these changes have occured correctly. However, when pulled from the REST based services, the values are not the same as those in the database. I am assuming this is because JPA has cached local copies of the database contents on the Glassfish server and JPA needs to refresh the associated entities.</p> <p>I have tried adding a couple of methods to the AbstractFacade class NetBeans generates:</p> <pre><code>public abstract class AbstractFacade&lt;T&gt; { private Class&lt;T&gt; entityClass; private String entityName; private static boolean _refresh = true; public static void refresh() { _refresh = true; } public AbstractFacade(Class&lt;T&gt; entityClass) { this.entityClass = entityClass; this.entityName = entityClass.getSimpleName(); } private void doRefresh() { if (_refresh) { EntityManager em = getEntityManager(); em.flush(); for (EntityType&lt;?&gt; entity : em.getMetamodel().getEntities()) { if (entity.getName().contains(entityName)) { try { em.refresh(entity); // log success } catch (IllegalArgumentException e) { // log failure ... typically complains entity is not managed } } } _refresh = false; } } ... }</code></pre> <p>I then call <code>doRefresh()</code> from each of the <code>find</code> methods NetBeans generates. What normally happens is the <code>IllegalArgumentsException</code> is thrown stating somethng like <code>Can not refresh not managed object: EntityTypeImpl@28524907:MyView [ javaType: class org.my.rest.MyView descriptor: RelationalDescriptor(org.my.rest.MyView --&gt; [DatabaseTable(my_view)]), mappings: 12].</code></p> <p>So I'm looking for some suggestions on how to correctly refresh the entities associated with the views so it is up to date.</p> <p><b>UPDATE:</b> Turns out my understanding of the underlying problem was not correct. It is somewhat related to <a href="https://stackoverflow.com/questions/12980332/glassfish-postgresql-table-with-composite-two-part-primary-key-generating-co">another question I posted earlier</a>, namely the view had no single field which could be used as a unique identifier. NetBeans required I select an ID field, so I just chose one part of what should have been a multi-part key. This exhibited the behavior that all records with a particular ID field were identical, even though the database had records with the same ID field but the rest of it was different. JPA didn't go any further than looking at what I told it was the unique identifier and simply pulled the first record it found.</p> <p>I resolved this by adding a unique identifier field (never was able to get the multipart key to work properly).</p>
    singulars
    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.
 

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