JPAContainer in a Editable Table

Hi,

I’ve been browsing forums and example for two days now and i can’t get something to work.

I simple have a JPAContainer, linked with a Entity. I set this container as a datasource for a editable table.

So far everything work fine, table get loaded with data and i can modify the data.

When i try to save is the probleme, nothing happend… nothing get saved… if i refresh the page, my changed value still there
but when i check in the database, nothing has really changed.

here is my simple code,
what’s wrong ?

Thanks

package ca.exagon.demovaadin.view;

import javax.persistence.EntityManager;

import ca.exagon.demovaadin.DemovaadinUI;
import ca.exagon.demovaadin.model.RrpadTag;

import com.vaadin.addon.jpacontainer.JPAContainer;
import com.vaadin.addon.jpacontainer.JPAContainerFactory;
import com.vaadin.data.Container.Filter;
import com.vaadin.data.util.filter.Compare;
import com.vaadin.server.VaadinSession;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Table;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Button.ClickEvent;

public class TestScope extends VerticalLayout {

private static final long serialVersionUID = 1L;

//final EntityManager em = (EntityManager) VaadinSession.getCurrent().getAttribute(“ENTITYMANAGER”);
//private JPAContainer JPAtag;

public TestScope() {
construirePage();
}

private void construirePage() {

final JPAContainer JPAtag = JPAContainerFactory.make(RrpadTag.class,DemovaadinUI.getPU());

// Some filter to reduce number of row…
Filter filter1 = new Compare.Greater(“patcleint”, 163800);
Filter filter2 = new Compare.Less(“patcleint”, 163900);
JPAtag.addContainerFilter(filter1);
JPAtag.addContainerFilter(filter2);

final Table table = new Table();
table.setImmediate(true);
table.setEditable(true);
table.setReadOnly(false);

table.setContainerDataSource(JPAtag);

table.setVisibleColumns(new Object {“patcleint”,“pattaucal”});

Button btnCommit = new Button(“Commit”);
btnCommit.addClickListener(new ClickListener() {

@Override
public void buttonClick(ClickEvent event) {
table.commit();
JPAtag.commit();

}
});

this.addComponent(table);
this.addComponent(btnCommit);

}

}

13313.java (1.79 KB)

After further test i found something else.

The binding seems to be alright, i try tryed the same code on another table and editing went fine, as far as the fields were texte.

In my above exemples, the field i am trying to update are numeric, my entity have been created using a tool and they created numbers with BigDecimals. When i mose over numeric field in the table i can see in red tooltip “Could not convert value to number”.

I can update any non numeric fields, but if i change one of those, nothing happend.

Any suggestion on how i could fix this problem ?

Should i rewrite manually my entitys and change numerics for Long ?
Can i change table property for those fields ?

Hi,

seems that something goes wrong when vaadin tries to convert your input string to number. Are you sure you’re inputting the number in correct format? To better see what actually goes wrong with the parsing, place a debug breakpoint to com.vaadin.data.util.converter.AbstractStringToNumberConverter.convertToNumber(String, Class<? extends Number>, Locale).

You could of course change your entities to use long if that helps, or once you know what goes wrong with the parsing you could create your own converter which avoids the issue.

If I remember correctly, the latest Vaadin version (7.1.8) should work with automatic conversion of Long.
The 7.2 branch might also have automatic conversions for BigDecimal, I’m not sure.

You can also create and register your own converters to the converter factory, or explicitly set a converter for a field.

You are both correct.

I’m still new to vaadin and i don’t know well all possibilities. I didn’t know what was a converter until fews hours ago.
I created a extended version of the default converter factory that support bigdecimal and set it up in my session at the application openening.

Until 7.2 come.

Thanks for the help.

Just another small question, after this fix everything work perfectly except for the key field. (i drop the whole entity in a table).
I don’t plan on changing id, but just for my personnal knowlegde, it is just because it’s the primary key that it didn’t work ?

Entity identity is based on the primary key, so normally it shouldn’t change and thus there are some restrictions to what you can do for it through a JPAContainer. If modifying a primary key arbitrarily would be allowed, the container would need some other way to track entity identites and notify its other parts of the changes etc.