Jpa Container通过表单添加项目

时间:2014-09-15 15:45:23

标签: java vaadin7 jpacontainer

我开始使用Vaadin 7.我想用简单的方法通过Vaadin 7创建许多实体表格和表格。

我的问题是 有代码

package com;

import com.vaadin.addon.jpacontainer.JPAContainer;
import com.vaadin.addon.jpacontainer.JPAContainerFactory;
import com.vaadin.data.fieldgroup.FieldGroup;
import com.vaadin.server.VaadinRequest;
import com.vaadin.ui.*;

public class TestVaadinApplication extends UI {

    @Override
    protected void init(VaadinRequest request) {
        VerticalLayout tableLayout = new VerticalLayout();

        final Table table = new Table("table with data from JPAContainer");
        final JPAContainer<UserEntity> userDataSource = JPAContainerFactory.makeJndi(UserEntity.class);
        table.setContainerDataSource(userDataSource);
        table.setSizeFull();
        tableLayout.addComponent(table);
        tableLayout.addComponent(new Button("add user entity", new Button.ClickListener() {
            @Override
            public void buttonClick(Button.ClickEvent event) {

                final Window editEntityWindow = new Window();
                editEntityWindow.setModal(true);
                editEntityWindow.setClosable(true);
                final FieldGroup fieldGroup = new FieldGroup(userDataSource.createEntityItem(new UserEntity()));
                FormLayout formLayout = new FormLayout();
                formLayout.addComponent(fieldGroup.buildAndBind("nameCaption", "name"));
                formLayout.addComponent(fieldGroup.buildAndBind("loginCaption", "login"));
                formLayout.addComponent(new Button("SaveEntity", new Button.ClickListener() {
                    @Override
                    public void buttonClick(Button.ClickEvent event) {
                        try {
                            fieldGroup.commit();
                            editEntityWindow.close();
                            table.refreshRowCache();
                        } catch (FieldGroup.CommitException e) {
                            e.printStackTrace();
                            Notification.show("Error!", Notification.Type.ERROR_MESSAGE);
                        }
                    }
                }));

                editEntityWindow.setContent(formLayout);

                TestVaadinApplication.this.addWindow(editEntityWindow);
            }
        }));


        setContent(tableLayout);
    }
}

当我按下SaveEntity按钮时,没有任何事情发生 - 实体没有添加。

Jpa工作正常。我做错了什么?

1 个答案:

答案 0 :(得分:1)

您使用的是Vaadin 7.3和JPAContainer 3.1.1吗?尝试添加 userDataSource.commit(); 在fieldGroup.commit();

之后