2013-07-10 18 views
1

我正在使用Vaadin爲單個數據庫表構建簡單編輯器,使用JPAContainer和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); 



} 

回答

0

好了,這是可以做到的,我追求的,但僅適用於帶有自動生成主鍵,由於Vaadin JPA容器限制的實體。 代碼我有預期的自動生成的id,但如果不是這種情況會失敗。

向容器添加新項目時,com.vaadin.ui.Table委託給它的容器,在未提供itemId的情況下調用.addItem(),或者在提供的情況下調用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(); 
} 

此代碼解釋,如果實體不能與數據庫中的一個空contstructor創建的,這將失敗的issue--。

Vaadin示例通過使用Form對象來收集數據,然後在數據完全填充後調用addEntity()方法來解決此問題。

相關問題