PostgreSQL的9.1 Glassfish的3.1.2.2 火狐10.0.0.7 Linux的
我使用JPA坚持实体的PostgreSQL。然而,有一个表(名为位置),这是由触发器填充,我使用该表充当仅读的实体。因为它的加载和触发器操纵的实体从来没有持续数据转换成此表。我可以将数据加载到我管理的bean,并查看职位表数据的罚款。
我的问题是,一旦数据库(位置表)已被触发修改,当位置表再次查询,值仍然是相同的。该位置的实体仍包含旧值并没有重新加载。
这里是处理中的地位的实体bean的。我加em.flush(),它并没有帮助,是不知道如何以这种方式使用em.refresh()。而实际上,如何将同步帮助反正,因为它不知道我想同步到没有什么查询。
任何帮助非常赞赏。
该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;
}
实体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>
在任何情况下运行到这一点,我学会了如何使用刷新()和这个固定我的问题。
pos = (Position) em.createNamedQuery("findPosition")
.setParameter("posId", posId).getSingleResult();
em.refresh(pos);
在我看来,正确的方法来创建更新只读实体“外部”如下:
@Access
到AccessType.FIELD
(或删除注解,因为它是默认的访问类型)作为this answer说出来。还建议仅作为this answer规定了公共干将。这防止从应用程序内beeing改性的实体。persistence.xml
文件选择性共享高速缓存模式并添加注解@Cacheable(false)
到实体类如this answer陈述出来。这迫使JPA当你调用EntityManager#find(...)
总是从持久层加载的实体。