Commit Data to Database using SQLContainer

Hi

I am new to Vaadin.

Trying to commit data to database with following code:


	public void valueChange(ValueChangeEvent event) {
		Property property = event.getProperty();
		if (property == personList) {
			final Item item = personList.getItem(personList.getValue());
			form = new Form();
			if (item != form.getItemDataSource()) {
				form.setItemDataSource(item);

				lay.removeComponent(personList);
				lay.removeAllComponents();
				HorizontalLayout footer = new HorizontalLayout();
				footer.setSpacing(true);
				footer.addComponent(saveContactForEdit);
				footer.addComponent(new Button("Cancle",
						new Button.ClickListener() {
							private static final long serialVersionUID = 8874945316629486111L;

							@Override
							public void buttonClick(ClickEvent event) {
								lay.removeAllComponents();
								removePersonForm();
								lay.addComponent(personList);

							}
						}));
				form.setFooter(footer);
				butnLay = new HorizontalLayout();
				lay.addComponent(form);
				lay.removeComponent(personForm);
				lay.addComponent(butnLay);

				saveContactForEdit.addListener(new Button.ClickListener() {
					private static final long serialVersionUID = 1L;

					@Override
					public void buttonClick(ClickEvent event) {
						
						//Item itm = personList.getItem(event.getSource());
						System.out.println("Calling Save Edited Data............");
						//getDataSource().removeAllItems();

						System.out.println("form -->  "+form);
						System.out.println("personForm -->  "+personForm);
						System.out.println("personList -->  "+personList);
						form.commit();					
						personForm.commit();
						personList.commit();
						lay.removeAllComponents();
						lay.addComponent(personForm);
						lay.addComponent(personList);
					}

				});

			}
		}
	}

It’s not committing data to database. i.e. changes are not being reflected to database.

Please help.

Thanks

What kind of Item do you use? I’m asking because I don’t see any database-related code here. So if you are not using an Item that does it by itself, your values are only commited to your Item and would have to be persisted from there using some custom code.

hi Tobias ,Thanks for reply ,me and B S are working on the same demo application. in above particular code functionality we want to edit item of table and update it in database.
In this demo aplication we use similar functinality as Address book demo aplication. code is :

 public void valueChange(ValueChangeEvent event) {
        Property property = event.getProperty();
        if (property == personList) {
            final Item item = personList.getItem(personList.getValue());
            form = new Form();
            if (item != form.getItemDataSource()) {
                form.setItemDataSource(item);

and on click of row of table form with the row columns as fields are populated.
and when we change the data and click on “Update” button following listener is called :[quote]
public void buttonClick(ClickEvent event) {
personForm.commit();}
[/quote]
as far we concern as item is populated from the table container it should have to be updated and even updated row can saw on the list (i.e. Table).But values are not updated in the database even though we called commit() . Is there anything missing ?please tell us.

Thanks In advance.

Well, I see a misunderstanding here: Most Containers and Items do NOT have anything to do with databases. I’ll try to explain the process with the most commonly used BeanItemContainer:

First you construct your Container and Items by calling the appropriate constructors. This binds the Items to an underlying Bean. Now you can for example use this Container / Item to populate a Table or a Form. Both of them use another abstraction-layer, the Field. A Field can be seen as the View, the Item is glue-code that abstracts (and sometimes even forms) your data-model, which is (in case of BeanItemContainer) formed by an Java-Bean. A Field does have TWO values: the Display-Value and the Value from the underlying datasource (which is an Item). The Display-Value represents exactly what the user typed in - no conversions or validations will be done on that. If you call commit(), the Display-value gets converted and validated and then eventually gets written to the item (which will write it into the underlying bean).

So after calling commit(), the Display-value just gets written into your data-model but (in most cases) gets not persisted into a database. This task will have to be done by yourself. Exceptions apply for specialized Containers like SQLContainer or JPAContainer, which indeed work directly with the database. This is why it is important to know which concrete Container-implementation you are using. Otherwise your question cannot be answered properly.

We are using SQLContainer.

Ok, I cannot help with the SQLContainer as I never used it. But I changed the topic to reflect that you have a question regarding SQLContainer so that the skilled people might see it.

EDIT: Ok, I’ve seen that only the Thread-title gets displayed in the “Recent Posts”-view, not the title of the last posting. @B S: You should update your initial posting to have a more meaningful topic. This would hopefully attract the right people to help you.