【问题标题】:Merging two database tables into a single Vaadin Treetable将两个数据库表合并到一个 Vaadin 树表中
【发布时间】:2015-04-08 19:55:44
【问题描述】:

TL;DR:我如何将来自两个数据库表的信息合并到一个 Vaadin 树表中(或者,当 Vaadin 7.5 发布时,一个层次网格)?


我目前有一个 Java Swing 桌面应用程序可以执行此操作,尽管使用每 30 秒从 SQL Server 更新的 Java Bean 的 ArrayLists 可能非常低效。好吧,我现在正尝试将此桌面应用程序移植到 Vaadin 网络应用程序。桌面应用程序具有登录功能,我最终会担心为 Web 应用程序做同样的事情,但现在,我只想尝试让这个 Web 应用程序最基本的部分正常工作:Treetable。或者,希望很快,一个层次网格。

为了帮助说明我的目标,我将尝试发布我创建的图像,该图像应该显示两个表中的数据需要如何合并到树表中(使用我现有桌面应用程序的部分屏幕截图) :

我非常了解如何在 SQL 中使用 JOIN 命令,并且我已经简要阅读了有关 Referencing Another SQLContainer 的内容,但我仍处于学习 Vaadin 的早期阶段,仍在尝试围绕 SQLContainerFreeformQuery,以及我需要如何为我的项目实施 FreeformStatementDelegate。更不用说我需要为每一行实现复选框,正如您在该照片中看到的那样,以便在单击它们时更新数据库。对于具有多个 OrderDetail 项目且仅完成其中一些 OrderDetail 项目的作业,复选框的半选中状态将是必要的。为了让我的 Java Swing 程序能够正常工作,我不得不依靠已经准备好大部分代码的专业 Java 开发人员,而且天哪,这是否超级复杂!

如果有人能给我一个关于如何完成这项任务的高级视图以及一些示例,我将不胜感激。我完全明白我在这里要求很多,只要你愿意,我愿意慢慢来,一步一步来。我真的很想完全理解这一点,所以我不只是不假思索地复制粘贴代码。

【问题讨论】:

    标签: sql-server database vaadin


    【解决方案1】:

    我从未使用过SQLContainer,所以这可能不是您想要的答案。我只是快速浏览了SQLContainer,但我不确定它是否符合您的目的。对于TreeTable,您将需要一个Container 实现Container.Hierarchical 接口,否则表格将在其周围放置一个包装器,您必须手动设置父子关系。您可能可以扩展 SQLContainer 并在该类中实现 Container.Hierarchical 中的方法,但这可能会变得复杂。

    在您的情况下,我想我会实现自己的容器,可能扩展AbstractContainer,以免费获取监听器代码,并实现Hierarchical。我知道有很多方法要实现,所以这需要一些时间,但是大多数方法都可以快速实现,您可以从基本方法开始并添加更多接口(@98​​7654332@,SortableIndexedFilterable, Collapsible,...) 稍后。
    如果处理得当,您最终会得到易于阅读的代码,可以在未来轻松扩展,并且您将不再依赖 SQLContainer 的未来版本。

    另一件好事是,您将学到很多关于 vaadin 中使用的数据结构(ContainerItemProperty)的知识。但正如我所说,我真的不知道SQLContainer 所以也许会有更好的答案告诉你SQLContainer 很容易

    对于复选框功能,您可以将名称/产品属性显示为CheckBox。使用图标和标题,它看起来几乎就像你想要的一样。请参阅http://demo.vaadin.com/sampler/#ui/data-input/other/check-box 并设置一个图标。半选中状态可以用css来完成。

    希望这可以帮助您找到适合您任务的解决方案。

    【讨论】:

    • 为什么扩展SQLContainer实现Container.Hierarchical会很复杂?
    • 这只是我根据我的经验得出的看法。这也可能很容易,但大多数时候我尝试扩展一个类,做一些该类没有做的事情,我最终会遇到相当复杂的情况。但是由于我不太了解SQLContainer,所以它可能会很好用。也许使用 UNION 查询而不是 JOIN 来合并列(例如 customer_name 和 product_id)并分配给相同的属性
    • 实际上我现在开始认为我应该使用JPAContainer 而不是SQLContainer,因为它开箱即用地实现了Container.Hierarchical。我现在正在阅读它并希望它不会是一个复杂的安装,特别是因为我仍然对 Ivy 和 Maven 一无所知。我真是个n00b。
    • 好吧@Sturm,我相信你很久以前就解决了这个问题,但我希望你最后没有像this is what they were saying about it那样在你发布这个的时候误会JPAContainer问题...
    • 实际上,@Pere,我从来没有这样做过。除了 Vaadin 似乎超出了我目前的联盟这一事实之外,我的雇主在过去的一年里让我背靠背地从事其他项目+。恐怕我还没有时间回到这个 Vaadin 项目,而按照我在空闲时间学习 JS、PHP、Ruby、Rails 等的速度,我可能只是创建我想要的 Web 应用程序而是从头开始使用这些技术。感谢JPAContainer 的提醒。我一定会记住这一点以供将来参考。
    【解决方案2】:

    我承认我自己是 vaadin 的初学者,可能有更好的方法可以做到这一点,但这是我模拟的一些似乎有效的方法。它并不能满足您的所有需求,但它可能是您开始的基础。最重要的是,要将更改保存回数据库,您需要在容器中的某些内容发生更改时更新 SQLContainers。

    import com.vaadin.data.Item;
    import com.vaadin.data.Property;
    import com.vaadin.data.util.HierarchicalContainer;
    import com.vaadin.data.util.sqlcontainer.SQLContainer;
    
    @SuppressWarnings("serial")
    public class TwoTableHierarchicalContainer extends HierarchicalContainer {
        private SQLContainer parentContainer;
        private SQLContainer childContainer;
        private String parentPrimaryKey;
        private String childForeignKey;
    
        public TwoTableHierarchicalContainer(SQLContainer parentContainer, SQLContainer childContainer,
                String parentPrimaryKey, String childForeignKey) {
            this.parentContainer = parentContainer;
            this.childContainer = childContainer;
            this.parentPrimaryKey = parentPrimaryKey;
            this.childForeignKey = childForeignKey;
            init();
        }
    
        private void init() {
            for (Object containerPropertyIds : parentContainer.getContainerPropertyIds()) {
                addContainerProperty(containerPropertyIds, Object.class, "");
            }
            for (Object containerPropertyIds : childContainer.getContainerPropertyIds()) {
                addContainerProperty(containerPropertyIds, Object.class, "");
            }
            for (Object itemId : parentContainer.getItemIds()) {
                Item parent = parentContainer.getItem(itemId);
                Object newParentId = parent.getItemProperty(parentPrimaryKey).getValue();
                Item newParent = addItem(newParentId);
                setChildrenAllowed(newParentId, false);
                for (Object propertyId : parent.getItemPropertyIds()) {
                    @SuppressWarnings("unchecked")
                    Property<Object> newProperty = newParent.getItemProperty(propertyId);
                    newProperty.setValue(parent.getItemProperty(propertyId).getValue());
                }
            }
            for (Object itemId : childContainer.getItemIds()) {
                Item child = childContainer.getItem(itemId);
                Object newParentId = child.getItemProperty(childForeignKey).getValue();
                Object newChildId = addItem();
                Item newChild = getItem(newChildId);
                setChildrenAllowed(newParentId, true);
                setParent(newChildId, newParentId);
                setChildrenAllowed(newChildId, false);
                for (Object propertyId : child.getItemPropertyIds()) {
                    @SuppressWarnings("unchecked")
                    Property<Object> newProperty = newChild.getItemProperty(propertyId);
                    newProperty.setValue(child.getItemProperty(propertyId).getValue());
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2018-06-23
      • 2015-10-29
      • 2020-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-06
      • 1970-01-01
      相关资源
      最近更新 更多