GWT RequestFactory在错误的对象上调用setter

克里斯·欣肖

首先,这不是错误的对象,但由于我已经诊断出问题,因此它不是正确的参考。

我有一个编辑器,在其中编辑一个对象,该对象包含另一个包含更多bean的列表编辑器。

我有一个TagCollectionProxy,并且其中包含TagProxy对象的列表。当我将TagCollectionProxy与附加的List对象一起保存时,会发生问题。我的实体定位器正在数据库中查找对象并设置值,但随后我没有对该对象的引用。这是新的,我不确定为什么现在会发生这种情况。

我在对象上保存时调用此调试消息。我在setValue的Tag类中添加了一条消息,您可以看到我在域实体上设置了正确的值。ID匹配。问题出现在我的dao类中的save(TagCollection)的下一条消息中。我正在遍历实体并打印值,但是该实体的值没有更改。

2014-08-29T15:34:15.912-0500|INFO: Setting value Tag2asdfasdfasdfasdf for id 294 Reference cpbx.rialto.dms.Tag@1135696
2014-08-29T15:34:15.944-0500|INFO: Tag name => asdfasdf Tag value => Tag2 id => 294 ref => cpbx.rialto.dms.Tag@1893889
2014-08-29T15:34:15.944-0500|INFO: Id of tag is 294

在此先感谢您的帮助,我正在按时脱发。

这是我的活动,用于保存对象。请注意,我正在保存包含Tag列表的TagCollectionProxy。

public class TagCollectionEditActivity extends AbstractActivity<TagCollectionEditView> implements
        TagCollectionEditView.Presenter {



    private Receiver<Long> saveReceiver = new Receiver<Long>() {
        @Override
        public void onSuccess(Long response) {
            logger.info("saved tagCollection with id of " + response);
            clientFactory.getPlaceController().goTo(new TagCollectionsPlace());
        }

        public void onConstraintViolation(Set<ConstraintViolation<?>> violations) {

            for (ConstraintViolation<?> violation : violations) {
                logger.warning(violation.getMessage());
            }
            getDisplay().getEditorDriver().setConstraintViolations(violations);
        }

        public void onFailure(ServerFailure failure) {
            logger.log(Level.SEVERE, "Failed to save tag collection " + failure.getMessage());
            getDisplay().showError(failure);
        }
    };

    private static final Logger logger = Logger.getLogger(TagCollectionEditActivity.class.getName());

    private IClientFactory clientFactory;

    private TagCollectionDaoRequest editContext = null;

    public TagCollectionEditActivity(IClientFactory clientFactory) {
        super(new TagCollectionEditView(clientFactory.getResources(), clientFactory.getEventBus()));
        this.clientFactory = clientFactory;
    }

    @Override
    protected void bindToView() {
        getDisplay().setPresenter(this);
        final TagCollectionEditPlace place = (TagCollectionEditPlace) getPlace();
        Long tagCollectionId = place.getTagCollectionId();

        if (tagCollectionId == null) {
            createTagCollection();
        } else {
            findCollection(tagCollectionId);
        }
    }

    private void findCollection(Long tagCollectionId) {
        clientFactory.tagCollectionRequest().find(tagCollectionId).with(getEntityProperties())
                .fire(new Receiver<TagCollectionProxy>() {

                    @Override
                    public void onSuccess(TagCollectionProxy tagCollection) {
                        editContext = clientFactory.tagCollectionRequest();
                        editContext.save(tagCollection).with(getDisplay().getEditorDriver().getPaths()).to(saveReceiver);
                        getDisplay().getEditorDriver().edit(tagCollection, editContext);
                        GWT.log("Context is " + editContext.hashCode());
                    }
                });
    }

    private void createTagCollection() {
        editContext = clientFactory.tagCollectionRequest();
        TagCollectionProxy tagCollection = editContext.create(TagCollectionProxy.class);
        editContext.save(tagCollection).with(getDisplay().getEditorDriver().getPaths()).to(saveReceiver);
        tagCollection.setTags(new ArrayList<TagProxy>());
        getDisplay().getEditorDriver().edit(tagCollection, editContext);
    }

    @Override
    public void onSave() {
        RequestContext context = getDisplay().getEditorDriver().flush();
        context.fire();
    }   

    public String[] getEntityProperties() {
        return new String[] { "tags", "deviceFamily" };
    }

    @Override
    public void onCancel() {
        clientFactory.getPlaceController().goTo(new TagCollectionsPlace());
    }
}

这是我的标签收集代理,在其中定义了DomainEntityLocator

@ProxyFor(value = TagCollection.class, locator = DomainEntityLocator.class)
public interface TagCollectionProxy extends DomainEntityProxy {

    public List<TagProxy> getTags();

    public void setTags(List<TagProxy> tags);

    public DeviceFamilyProxy getDeviceFamily();

    public void setDeviceFamily(DeviceFamilyProxy deviceFamily);

}

这是我的定位器,它使用数据库通过JPAEntityManager查找对象。

public class DomainEntityLocator extends Locator<DomainEntity, Long> {

    private static EntityManager em = null;

    static {
        Context initCtx;
        try {
            initCtx = new InitialContext();
            // perform JNDI lookup to obtain container-managed entity manager
            em = (javax.persistence.EntityManager) initCtx.lookup("java:comp/env/persistence/DomainEntityManager");

        } catch (NamingException e) {
            throw new RuntimeException("Unable to get the domain entity manager");
        }
    }

    public DomainEntityLocator() {
    }

    @Override
    public DomainEntity create(Class<? extends DomainEntity> clazz) {
        try {
            return clazz.newInstance();
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public DomainEntity find(Class<? extends DomainEntity> clazz, Long id) {
        return em.find(clazz, id);
    }

    @Override
    public Class<DomainEntity> getDomainType() {
        return DomainEntity.class;
    }

    @Override
    public Long getId(DomainEntity domainObject) {
        return domainObject.getId();
    }

    @Override
    public Class<Long> getIdType() {
        return Long.class;
    }

    @Override
    public Object getVersion(DomainEntity domainObject) {
        return 0;
    }
}

更新

经过一番调查,我意识到服务器端的JPA是我的问题的罪魁祸首。我得出的结论是,DomainEntityLocator正在获取实体,但没有将其包装在事务中。因此,DomainEntityLocator#find()方法将检索实体,而ServiceLayer将修改实体上的值。我的问题是find方法没有包装在事务中,并且由于我要检索的实体管理器没有被管理,因此实体管理器没有刷新更改。因此,我看不到该值的更新。

关于主题问题2

是否有向Entity Locator添加事务管理的通用模式,以便它将在请求工厂调用设置器之后保留该对象。我曾经用一个简单的servlet过滤器来做到这一点,但是我正在从事的项目需要java ee来维护这种功能。由于ServiceLayer查找它的方式,我无法将DomainEntityLocator设置为无状态Bean。最好扩展RequestFactoryServlet并将doPost包装成透明文字吗?这似乎是最合乎逻辑的想法。有什么建议?

再次感谢托马斯

托马斯·布罗耶

在服务器端,RequestFactory会使用您的定位器按对象的ID来加载每个对象,因此TagCollection将加载(带有的列表Tag),Tag也将使用来加载每个对象DomainEntityLocator(假设这是它们在声明的方式@ProxyFor)。
的制定者将被要求由定位器返回的对象,而不是从的那些TagCollectiongetTags名单。

您必须确保它们是相同的实例。

对于JPA,这意味着使用“打开的视图中的会话”(也称为“每个请求的会话”)模式。JPA会话使用缓存,因此在整个HTTP请求中共享同一会话将确保TagTagCollection或直接从其ID加载时使用的相同实例

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章