触发器更新后如何将实体Bean与数据库同步 [英] How to Sync Entity Bean with Database After Trigger Update

查看:167
本文介绍了触发器更新后如何将实体Bean与数据库同步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PostgreSQL 9.1

Glassfish 3.1.2.2

Firefox 10.0.0.7

Linux

PostgreSQL 9.1
Glassfish 3.1.2.2
Firefox 10.0.0.7
Linux

我正在使用JPA来保留PostgreSQL中的实体。然而,有一个表(名为Position)由触发器填充,我使用该表的实体作为只读。实体从触发器加载和处理时,不会将数据保留在此表中。我可以将数据加载到我的托管bean中,并查看位置表数据。

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.

这是处理Position实体的bean。我添加了没有帮助的em.flush(),并且不知道如何以这种方式使用em.refresh()。实际上,如果同步无法帮助,因为它不知道什么我想要同步到没有查询。

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.

任何帮助非常感谢。

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 ...

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.xml

<persistence-unit name="positionbean-pu" transaction-type="JTA">
    <jta-data-source>jdbc/postgreSQLPool</jta-data-source>
</persistence-unit>


推荐答案

如果有人遇到这种情况,我学会了如何使用refresh(),这修复了我的问题。

In case anyone runs into this, I learned how to use refresh() and this fixed my problem.

        pos = (Position) em.createNamedQuery("findPosition")
            .setParameter("posId", posId).getSingleResult();

        em.refresh(pos);

这篇关于触发器更新后如何将实体Bean与数据库同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆