使用JPA容器时,如何在Vaadin表中创建新的内嵌项?
我正在使用Vaadin为单个数据库表构建一个简单的编辑器,使用JPAContainer和一个Vaadin表组件。删除和编辑项目工作正常,但我在添加项目时遇到问题 Vaadin教程解释了如何使用弹出窗口添加项,但我希望通过使用表中的新空行来添加项。所需的功能是为用户提供一行一些默认数据,然后允许用户在对新行中的数据满意后保存数据 我面临的问题是,当我试图做我认为应该有效的事情时,我得不到支持 我的表已设置并绑定到位置实体的JPA容器:使用JPA容器时,如何在Vaadin表中创建新的内嵌项?,jpa,vaadin,Jpa,Vaadin,我正在使用Vaadin为单个数据库表构建一个简单的编辑器,使用JPAContainer和一个Vaadin表组件。删除和编辑项目工作正常,但我在添加项目时遇到问题 Vaadin教程解释了如何使用弹出窗口添加项,但我希望通过使用表中的新空行来添加项。所需的功能是为用户提供一行一些默认数据,然后允许用户在对新行中的数据满意后保存数据 我面临的问题是,当我试图做我认为应该有效的事情时,我得不到支持 我的表已设置并绑定到位置实体的JPA容器: private JPAContainer locations
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()源代码:
好的,可以执行我想要的操作,但由于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();
}
/**
*此系统不完全支持此功能
*实现。实现尝试调用空参数
*构造函数并将实体添加到数据库中。如果标识符不是
*自动生成的或空参数构造函数不存在
*操作将失败并引发UnSupportedOperationException。
*
*{@inheritardoc}
*/
公共对象addItem()引发UnsupportedOperationException{
试一试{
T newInstance=getEntityClass().newInstance();
对象id=加法(newInstance);
返回id;
}捕获(实例化异常e){
}捕获(非法访问例外e){
}
抛出新的UnsupportedOperationException();
}
这段代码解释了这个问题——如果无法在数据库中使用null contstructor创建实体,则此操作将失败
Vaadin示例通过使用Form对象收集数据来解决这个问题,然后在数据完全填充后调用addEntity()方法
/**
* <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();
}