Important Notice - Forums is archived
To simplify things and help our users to be more productive, we have archived the current forum and focus our efforts on helping developers on Stack Overflow. You can post new questions on Stack Overflow or join our Discord channel.

Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.
Problem when commiting FieldGroup
Hi Vaadinians,
I am having a problem in commiting my data to my MYSQL Database.
Although the commit() method of the FieldGroup is executing but it does not updating the database data.
how do I solve this ?
Below is my code.
public class AccountInfo extends VerticalLayout {
private TextField fname;
private TextField mname;
private TextField lname;
private DateField birthdate;
private OptionGroup sex;
private TextField contact;
private TextField email;
private TextArea address;
private EntityManagerFactory emfactory = Persistence.createEntityManagerFactory(AdmincdoUI.PERSISTENT_UNIT);
private EntityManager entityManager = emfactory.createEntityManager();
private BeanItem<Information> bean;
private FieldGroup fieldGroup;
public AccountInfo() {
// TODO Auto-generated constructor stub
setSizeFull();
setMargin(new MarginInfo(false, true, false, true));
this.init();
HorizontalLayout subLayout = new HorizontalLayout();
subLayout.setMargin(true);
subLayout.setSizeUndefined();
subLayout.addComponent(this.information());
subLayout.addComponent(this.importantInfo());
addComponent(subLayout);
setComponentAlignment(getComponent(0), Alignment.TOP_CENTER);
addComponent(this.actionsBtn());
setComponentAlignment(getComponent(1), Alignment.TOP_CENTER);
}
private FormLayout information () {
FormLayout form = new FormLayout();
form.setWidth("500px");
form.addStyleName(ValoTheme.FORMLAYOUT_LIGHT);
Label label = new Label("Personal Info");
label.addStyleName(ValoTheme.LABEL_H2);
label.addStyleName(ValoTheme.LABEL_COLORED);
form.addComponent(label);
form.addComponent(this.fname);
form.addComponent(this.mname);
form.addComponent(this.lname);
form.addComponent(this.sex);
form.addComponent(this.birthdate);
return form;
}
private FormLayout importantInfo () {
FormLayout form = new FormLayout();
form.setWidth("500px");
form.addStyleName(ValoTheme.FORMLAYOUT_LIGHT);
Label label = new Label("Important Information");
label.addStyleName(ValoTheme.LABEL_H2);
label.addStyleName(ValoTheme.LABEL_COLORED);
form.addComponent(label);
form.addComponent(this.contact);
form.addComponent(this.email);
form.addComponent(this.address);
return form;
}
private HorizontalLayout actionsBtn () {
HorizontalLayout layout = new HorizontalLayout();
layout.setSizeUndefined();
layout.setSpacing(true);
Button btn = new Button("Save Edit");
btn.setIcon(FontAwesome.PENCIL);
btn.addStyleName(ValoTheme.BUTTON_PRIMARY);
btn.addListener(ClickEvent.class, this, "saveListener");
layout.addComponent(btn);
btn = new Button("Cancel");
btn.setIcon(FontAwesome.REFRESH);
btn.addListener(ClickEvent.class, this, "cancelListener");
layout.addComponent(btn);
return layout;
}
private void init() {
Query query = entityManager.createNamedQuery("find info");
query.setParameter("infoid", Main.session);
List<Information> userDetail = query.getResultList();
if( ! userDetail.isEmpty() ) {
for(Information in: userDetail) {
this.bean = new BeanItem<Information>(in);
this.fieldGroup = new FieldGroup(this.bean);
this.fieldGroup.setBuffered(false);
this.fname = new TextField("First name");
// this.fname.setPropertyDataSource(this.bean.getItemProperty("fname"));
// this.fname.setImmediate(true);
this.mname = new TextField("Middle name");
// this.mname.setPropertyDataSource(this.bean.getItemProperty("mname"));
// this.mname.setImmediate(true);
this.lname = new TextField("Last name");
// this.lname.setPropertyDataSource(this.bean.getItemProperty("lname"));
// this.lname.setImmediate(true);
this.birthdate = new DateField("Birthday");
// this.birthdate.setPropertyDataSource(this.bean.getItemProperty("birthdate"));
// this.birthdate.setImmediate(true);
this.sex = new OptionGroup("Gender");
// this.sex.setPropertyDataSource(this.bean.getItemProperty("gender"));
// this.sex.setImmediate(true);
this.sex.addItems("Female", "Male");
this.sex.addStyleName(ValoTheme.OPTIONGROUP_HORIZONTAL);
this.contact = new TextField("Contact Number");
// this.contact.setPropertyDataSource(this.bean.getItemProperty("contact"));
// this.contact.setImmediate(true);
this.email = new TextField("E-mail Address");
// this.email.setPropertyDataSource(this.bean.getItemProperty("email"));
// this.email.setImmediate(true);
this.address = new TextArea("Address");
// this.address.setPropertyDataSource(this.bean.getItemProperty("address"));
// this.address.setImmediate(true);
this.address.setRows(3);
this.fieldGroup.bind(this.fname, "fname");
this.fieldGroup.bind(this.mname, "mname");
this.fieldGroup.bind(this.lname, "lname");
this.fieldGroup.bind(this.sex, "gender");
this.fieldGroup.bind(this.birthdate, "birthdate");
this.fieldGroup.bind(this.contact, "contact");
this.fieldGroup.bind(this.email, "email");
this.fieldGroup.bind(this.address, "address");
}
}
}
public void saveListener (ClickEvent event) {
try {
// BeanItem<Information> a = fieldGroup.getItemDataSource();
fieldGroup.commit();
Notification.show("Your update was successfuly change!", Notification.TYPE_HUMANIZED_MESSAGE);
} catch (CommitException e) {
Notification.show("Correct the coresponding error!", Notification.TYPE_ERROR_MESSAGE);
}
}
public void cancelListener (ClickEvent event) {
fieldGroup.discard();
}
}
Hi,
The FieldGroup.commit does nothing related to actual DB commit. Actually I'd suggest NOT to use the buffered mode in FieldGroup at all unless you have a specific reason to do so. The entity object is your "buffer". Then in Save listener you should call EntityManager.persist/merge to actually save your changes into DB.
As a general advice I'd also suggest to move the JPA access to a separate helper class, that you'll just use from your Vaadin code. Check out for example this example CRUD app. It uses Java EE and the JPA implementation provided by it and also a really handy helper called DeltaSpike Data, but the same principle is still valid if you'd be using JPA in Java SE style.
cheers,
matti