PostgreSQL 9.1
Glassfish 3.1.2.2
Firefox 10.0.0.7
Linux
I am using JPA to persist entities in PostgreSQL. However, there is one table (named Position) which is populated by triggers and I use an entity of that table which acts as a read only. The entity never persist data into this table as it's loaded and manipulated from the triggers. I can load the data into my managed bean and view the Position table data fine.
The problem I have is that once the database (Position table) has been modified by the triggers, when the Position table is queried again, the values are still the same. The Position entity still contains the old values and have not reloaded.
Here is the bean which handles the Position entity. I added em.flush() which didn't help and wasn't sure how to use em.refresh() in this way. And actually, how would syncing help anyways since it doesn't know what I want to sync to without a query.
Any help much appreciated.
The EJB ...
@Stateless
public class PositionBean implements IPosition {
@PersistenceContext(unitName="positionbean-pu")
private EntityManager em;
public Position getPositionById(Integer posId) {
Position pos = null;
try {
pos = (Position) em.createNamedQuery("findPosition")
.setParameter("posId", posId).getSingleResult();
}
catch (Exception e) {
throw new EJBException(e.getMessage());
}
return pos;
}
The entity bean ...
@Entity
@SequenceGenerator(name="posIdGen", initialValue=10000,
sequenceName="pos_seq", allocationSize=1)
@NamedQuery(name="findPosition",
query="SELECT p FROM Position p WHERE p.posId = :posId")
@Table(name="Position")
public class Position implements Serializable {
// ...
persistence.xml
<persistence-unit name="positionbean-pu" transaction-type="JTA">
<jta-data-source>jdbc/postgreSQLPool</jta-data-source>
</persistence-unit>