【问题标题】:How do you create a new in-line item in a Vaadin Table when using JPAcontainer?使用 JPAcontainer 时如何在 Vaadin 表中创建新的内联项?
【发布时间】:2013-07-10 17:52:19
【问题描述】:

我正在使用 Vaadin 为单个数据库表构建一个简单的编辑器,使用 JPAContainer 和一个 Vaadin 表组件。删除和编辑项目工作正常,但我在添加项目时遇到问题。

Vaadin 教程解释了如何使用弹出窗口添加项目,但我想通过使用表格中的新空白行来添加项目。所需的功能是为用户提供一些默认数据的行,然后允许用户在对新行中的数据满意后保存数据。

我遇到的问题是,当我尝试做我认为应该工作的事情时,我得到了 UnsupportedOperation。

我的表已设置并绑定到位置实体的 JPAContainer:

private JPAContainer locations = JPAContainerFactory.make(Location.class,PERSISTENCE_UNIT);

我将表设置为缓冲,这样在用户单击保存按钮之前我不会将数据保存到数据库中:

    locationTable = new Table(null,locations);
    locationTable.setVisibleColumns(new Object[]{ "id","x","y","z","createDate","lastModDate"  } );
    locationTable.setSelectable(true);
    locationTable.setSizeFull();
    locationTable.setImmediate(true);
    locationTable.setBuffered(true);
    locationTable.setVisible(true);
    locationTable.setEditable(true);

.. 保存按钮将更改保存到数据库:

    Button saveButton = new Button("Save Changes");
    saveButton.addClickListener(new Button.ClickListener() {
        public void buttonClick(ClickEvent event) {
            locations.commit();
            statusLabel.setCaption("Changes Saved");
        }
    });

然后我绑定一个尝试添加新按钮的按钮。

    Button addButton = new Button("Add An Item");
    addButton.addClickListener(new Button.ClickListener() {
        public void buttonClick(ClickEvent event) {

            Object newkey = locationTable.addItem(new Object[]{"NEWLOCATION","0","0","0","7/10/2013","7/10/2013"}, "NEWLOCATION");
            locationTable.select(newkey);
            statusLabel.setCaption("Item Added. Populate Data and click Save to make permanent.");
        }
    });

单击 additem 按钮会引发 UnSupportedOperationException。

我已尝试调用 addItem 的其他变体,但无法正常工作。有人知道如何通过使用表格中的新行在表格中创建新项目吗?

完整的 init() 源代码:

@Override
protected void init(VaadinRequest request) {

    final VerticalLayout layout = new VerticalLayout();
    layout.setMargin(true);
    setContent(layout);
    final Label statusLabel = new Label("");

    locationTable = new Table(null,locations);
    locationTable.setVisibleColumns(new Object[]{ "id","x","y","z","createDate","lastModDate"  } );
    locationTable.setSelectable(true);
    locationTable.setSizeFull();
    locationTable.setImmediate(true);
    locationTable.setBuffered(true);
    locationTable.setVisible(true);
    locationTable.setEditable(true);

    Button saveButton = new Button("Save Changes");
    saveButton.addClickListener(new Button.ClickListener() {
        public void buttonClick(ClickEvent event) {
            locations.commit();
            statusLabel.setCaption("Changes Saved");
        }
    });

    Button addButton = new Button("Add An Item");
    addButton.addClickListener(new Button.ClickListener() {
        public void buttonClick(ClickEvent event) {

            Object newkey = locationTable.addItem(new Object[]{"NEWLOCATION","0","0","0","7/10/2013","7/10/2013"}, "NEWLOCATION");
            locationTable.select(newkey);
            statusLabel.setCaption("Item Added. Populate Data and click Save to make permanent.");
        }
    });
    Button deleteButton = new Button("Delete Selected");
    deleteButton.addClickListener(new Button.ClickListener() {
        public void buttonClick(ClickEvent event) {
             locations.removeItem(locationTable.getValue());
             statusLabel.setCaption("Item Removed. Click Save to make permanent.");
        }
    });


    layout.addComponent(locationTable );
    layout.addComponent(saveButton);
    layout.addComponent(addButton);
    layout.addComponent(deleteButton);
    layout.addComponent(statusLabel);



}

【问题讨论】:

    标签: jpa vaadin


    【解决方案1】:

    好的,由于 Vaadin JPA 容器的限制,可以执行我想要的操作,但仅限于具有自动生成主键的实体。 我的代码使用自动生成的 id 按预期工作,但如果不是这种情况,则会失败。

    向容器添加新项目时,com.vaadin.ui.Table 委托给其容器,在未提供 itemId 的情况下调用 .addItem(),在提供 itemId 的情况下调用 getItem()。

    对于新项目,调用 .addItem():

    /**
     * <strong>This functionality is not fully supported by this
     * implementation.</strong> The implementation tries to call empty parameter
     * constructor and add entity as such to database. If identifiers are not
     * autogenerated or empty parameter constructor does not exist, the
     * operation will fail and throw UnSupportedOperationException.
     * <p>
     * {@inheritDoc }
     */
    public Object addItem() throws UnsupportedOperationException {
        try {
            T newInstance = getEntityClass().newInstance();
            Object id = addEntity(newInstance);
            return id;
        } catch (InstantiationException e) {
        } catch (IllegalAccessException e) {
        }
        throw new UnsupportedOperationException();
    }
    

    这段代码解释了这个问题——如果无法在数据库中使用空构造函数创建实体,这将失败。

    Vaadin 示例通过使用 Form 对象收集数据来解决此问题,然后在完全填充数据后调用 addEntity() 方法。

    【讨论】:

      猜你喜欢
      • 2012-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-09
      • 1970-01-01
      • 2013-06-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多