异步回调-gwt

发布于 2024-08-25 15:13:43 字数 2162 浏览 12 评论 0原文

我在我的项目中使用 gwt 和 postgres。在前端,我有几个小部件,当我单击“保存项目”按钮时,我试图将其数据保存到后端的表中(这也采用创建的项目的名称)。

在异步回调部分中,我设置了多个表。但它没有正确发送数据。我收到以下错误:

org.postgresql.util.PSQLException: ERROR: insert or update on table "entitytype" violates foreign key constraint "entitytype_pname_fkey"
  Detail: Key (pname)=(Project Name) is not present in table "project".

但是当我在项目表上执行 select 语句时,我可以看到项目名称存在。

回调部分如下所示:

        oksave.addClickHandler(new ClickHandler(){
                            @Override
                            public void onClick(ClickEvent event) {
                                if(erasync == null)
                                    erasync = GWT.create(EntityRelationService.class);
                                AsyncCallback<Void> callback = new AsyncCallback<Void>(){
                                    @Override
                                    public void onFailure(Throwable caught) {
                                        }

                                    @Override
                                    public void onSuccess(Void result){  }                          
                        };      
    erasync.setProjects(projectname, callback);

                                for(int i = 0; i < boundaryPanel.getWidgetCount(); i++){

                                    top = new Integer(boundaryPanel.getWidget(i).getAbsoluteTop()).toString();
                                    left = new Integer(boundaryPanel.getWidget(i).getAbsoluteLeft()).toString();
                                    if(widgetTitle.startsWith("ATTR")){
                                        type = "regular";

                                            erasync.setEntityAttribute(name1, name, type, top, left, projectname, callback);
                                        }   else{
erasync.setEntityType(name, top, left, projectname, callback);
}                           
                                    }
    }

问题:

  1. 在异步回调中设置多个表(其中所有其他表都依赖于某个特定表)是否错误?
  2. 当我在上面的代码中说 setProjects 时,它不是先完成然后转到下一个吗?

请任何意见,我们将不胜感激。

谢谢。

I am using gwt and postgres for my project. On the front end i have few widgets whose data i am trying to save on to tables at the back-end when i click on "save project" button(this also takes the name for the created project).

In the asynchronous callback part i am setting more than one table. But it is not sending the data properly. I am getting the following error:

org.postgresql.util.PSQLException: ERROR: insert or update on table "entitytype" violates foreign key constraint "entitytype_pname_fkey"
  Detail: Key (pname)=(Project Name) is not present in table "project".

But when i do the select statement on project table i can see that the project name is present.

Here is how the callback part looks like:

        oksave.addClickHandler(new ClickHandler(){
                            @Override
                            public void onClick(ClickEvent event) {
                                if(erasync == null)
                                    erasync = GWT.create(EntityRelationService.class);
                                AsyncCallback<Void> callback = new AsyncCallback<Void>(){
                                    @Override
                                    public void onFailure(Throwable caught) {
                                        }

                                    @Override
                                    public void onSuccess(Void result){  }                          
                        };      
    erasync.setProjects(projectname, callback);

                                for(int i = 0; i < boundaryPanel.getWidgetCount(); i++){

                                    top = new Integer(boundaryPanel.getWidget(i).getAbsoluteTop()).toString();
                                    left = new Integer(boundaryPanel.getWidget(i).getAbsoluteLeft()).toString();
                                    if(widgetTitle.startsWith("ATTR")){
                                        type = "regular";

                                            erasync.setEntityAttribute(name1, name, type, top, left, projectname, callback);
                                        }   else{
erasync.setEntityType(name, top, left, projectname, callback);
}                           
                                    }
    }

Question:

  1. Is it wrong to set more than one in the asynchronous callback where all the other tables are dependent on a particular table?
  2. when i say setProjects in the above code isn't it first completed and then moved on to the next one?

Please any input will be greatly appreciated.

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

农村范ル 2024-09-01 15:13:43

使用该外键约束,您必须确保erasync.setProjects(...) 已完成,然后再插入其余内容。

我建议在 onsuccess 回调中(或从中)执行erasync.setEntityAttribute(...) 魔法,而不是直接跳到它。

With that foreign key constraint, you must make sure the erasync.setProjects(...) has completed before you insert the rest of the stuff.

I suggest doing the erasync.setEntityAttribute(...) magic in (or from) an onsuccess callback instead of jumping right to it.

流心雨 2024-09-01 15:13:43

您正在触发几个请求,其中(从错误消息猜测)确实应该按顺序调用。

任何时候你调用多个rpc调用;尝试认为您应该能够以任何顺序重新排列它们(因为这几乎是实际发生的情况,因为它们是异步的)...如果以相反的顺序运行它们没有意义;你不能连续解雇他们!

有两种方法可以解决您的问题:

嵌套:

    service.callFirst(someData, new AsyncCallback<Void> callback = new AsyncCallback<Void>(){
        @Override
        public void onFailure(Throwable caught) {/*Handle errors*/}
        @Override
        public void onSuccess(Void result){
            service.callSecond(someOtherData, new AsyncCallback<Void> callback = new AsyncCallback<Void>(){ 
                /* onSuccess and onFailure for second callback here */ 
            });
        }                       
    });

或者创建一个同时执行这两种操作的服务调用(推荐):

    service.callFirstAndSecond(someData, someOtherData, new AsyncCallback<Void> callback = new AsyncCallback<Void>(){
        @Override
        public void onFailure(Throwable caught) {/*Handle errors*/}
        @Override
        public void onSuccess(Void result){
            /* Handle success */
        }                       
    });

第二个选项很可能不会那么混乱,因为几个嵌套的异步回调很快就会变得非常广泛和令人困惑,而且您也可以只做一个请求。

You're firing several request in which (guessing from the error message) really should be called in sequence.

Any time you call more than one rpc call; try to think that you should be able to rearrange them in any order (because that's allmost what actually happens because they're asynchronous)... If running them in reverse order does not make sense; you cannot fire them sequentially!

Two ways to fix your problem:

Nesting:

    service.callFirst(someData, new AsyncCallback<Void> callback = new AsyncCallback<Void>(){
        @Override
        public void onFailure(Throwable caught) {/*Handle errors*/}
        @Override
        public void onSuccess(Void result){
            service.callSecond(someOtherData, new AsyncCallback<Void> callback = new AsyncCallback<Void>(){ 
                /* onSuccess and onFailure for second callback here */ 
            });
        }                       
    });

Or creating one service call that does both (Recommended):

    service.callFirstAndSecond(someData, someOtherData, new AsyncCallback<Void> callback = new AsyncCallback<Void>(){
        @Override
        public void onFailure(Throwable caught) {/*Handle errors*/}
        @Override
        public void onSuccess(Void result){
            /* Handle success */
        }                       
    });

The second option is most likely going to be much less messy, as several nested asynch callbacks quickly grows quite wide and confusing, also you make just one request.

×眷恋的温暖 2024-09-01 15:13:43

由于异步的性质,不要假设 setProjects(...) 方法将在 setEntityAttributesetEntityType 之前在服务器上调用。

就我个人而言,我更喜欢有一个 Project 类,其中包含所有必要的信息,例如:

public class Project{  
      private String projectName;  
      private List attributes = new ArrayList();  
      .. other properties  

  // Getter & Setter methods  
}

然后在一次往返中发送到服务器:

Project project = new Project();  
project.setProjectName(..);  
// Set other properties  
erasync.saveProjects(project, callback);

Because of nature of Async, don't assume setProjects(...) method will be called on the server before setEntityAttribute or setEntityType.

Personally, I prefer to have a Project class which contains all necessary info, for example:

public class Project{  
      private String projectName;  
      private List attributes = new ArrayList();  
      .. other properties  

  // Getter & Setter methods  
}

Then send to the server in one round trip:

Project project = new Project();  
project.setProjectName(..);  
// Set other properties  
erasync.saveProjects(project, callback);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文